| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| Can someone please guide me a link on java inner classes ?. I am having a tough time getting the nuances of the access mechanism for outer and inner class variables. thanks, milon |
|
#2
| |||
| |||
| katyusha wrote: > > > Can someone please guide me a link on java inner classes ?. I am > having a tough time getting the nuances of the access mechanism for > outer and inner class variables. > http://java.sun.com/docs/books/tutor...erclasses.html BugBear |
|
#3
| |||
| |||
| On 27-8-2008 17:34, bugbear wrote: > katyusha wrote: >> >> >> Can someone please guide me a link on java inner classes ?. I am >> having a tough time getting the nuances of the access mechanism for >> outer and inner class variables. >> > > http://java.sun.com/docs/books/tutor...erclasses.html > > BugBear Probably OP should start reading one page earlier to get a completer picture: <http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html>. This page refers to <http://blogs.sun.com/darcy/entry/nested_inner_member_and_top>, which gives an even more detailed description of the relations of the various types of classes. -- Regards, Roland |
|
#4
| |||
| |||
| Roland de Ruiter wrote: > On 27-8-2008 17:34, bugbear wrote: >> katyusha wrote: >>> >>> >>> Can someone please guide me a link on java inner classes ?. I am >>> having a tough time getting the nuances of the access mechanism for >>> outer and inner class variables. >>> >> >> http://java.sun.com/docs/books/tutor...erclasses.html >> >> BugBear > > Probably OP should start reading one page earlier to get a completer > picture: > <http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html>. This > page refers to > <http://blogs.sun.com/darcy/entry/nested_inner_member_and_top>, which > gives an even more detailed description of the relations of the various > types of classes. Yeah - most likely - I just hit "java inner classes tutorial" in google, and cut 'n' pasted the first hit, which appears to be more than the OP did. BugBear |
|
#5
| |||
| |||
| katyusha wrote: > > > Can someone please guide me a link on java inner classes ?. https://java.sun.com/docs/books/tuto...OO/nested.html > I am > having a tough time getting the nuances of the access mechanism for > outer and inner class variables. > Maybe you could compose some example code that shows the aspects you are having problems with? -- RGB |
|
#6
| |||
| |||
| bugbear wrote: > Roland de Ruiter wrote: >> On 27-8-2008 17:34, bugbear wrote: >>> katyusha wrote: >>>> >>>> >>>> Can someone please guide me a link on java inner classes ?. I am >>>> having a tough time getting the nuances of the access mechanism for >>>> outer and inner class variables. >>>> >>> >>> http://java.sun.com/docs/books/tutor...erclasses.html >>> >>> BugBear >> >> Probably OP should start reading one page earlier to get a completer >> picture: >> <http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html>. >> This page refers to >> <http://blogs.sun.com/darcy/entry/nested_inner_member_and_top>, which >> gives an even more detailed description of the relations of the >> various types of classes. > > Yeah - most likely - I just hit "java inner classes tutorial" > in google, and cut 'n' pasted the first hit, which appears to be more > than the OP did. > > BugBear Actually that tutorial has an error. Despite what it says, all nested ("static nested"), inner, or local classes do have a trust relationship with the outer ("enclosing") class: <sscce> class Outer { private int someNum = 17; public static void main ( String [] args ) { Outer.Inner inner = new Outer.Inner(); inner.showNum(); } private static class Inner { private void showNum () { Outer outer = new Outer(); System.out.println( outer.someNum ); // displays 17 } } } </sscce> -Wayne |
|
#7
| |||
| |||
| On Wed, 27 Aug 2008 08:00:12 -0700 (PDT), katyusha <milon87@gmail.com> wrote, quoted or indirectly quoted someone who said : >Can someone please guide me a link on java inner classes ?. I am >having a tough time getting the nuances of the access mechanism for >outer and inner class variables. I have written some essays to help get you started. See http://mindprod.com/jgloss/innerclasses.html then follow the links at the bottom to the related essays. -- Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com |
|
#8
| |||
| |||
| I was playing around with inner classes and was hopping someone could explain the following. Given: import java.lang.reflect.*; public class NestedScope { private int num = 17; public static void main ( String [] args ) { NestedScope outer = new NestedScope(); outer.demo(); } private void demo () { Inner inner = new Inner(); inner.showNum(); } private class Inner { private void showNum () { System.out.println( num ); // Displays 17 } } } This works fine when compiled and run. But, I was cleaning it up when I was done and noticed that javac had created an extra class file: C:\Temp>del *.class C:\Temp>javac NestedScope.java C:\Temp>dir *.class Volume in drive C has no label. Volume Serial Number is XXXX-XXXX Directory of C:\Temp 08/30/2008 02:28 PM 184 NestedScope$1.class 08/30/2008 02:28 PM 756 NestedScope$Inner.class 08/30/2008 02:28 PM 671 NestedScope.class 3 File(s) 1,611 bytes 0 Dir(s) 3,213,533,184 bytes free C:\Temp>javap NestedScope$1 Compiled from "NestedScope.java" class NestedScope$1 extends java.lang.Object{ } C:\Temp>del NestedScope$1.class C:\Temp>java NestedScope 17 C:\Temp>javac -version javac 1.6.0_07 C:\Temp> "NestedScope$1" has no fields, constructors, or methods of it's own. As the above shows it isn't used at all when the program is run. Comment out the object creation (the whole body) of the "demo" method, and then re-compiling, and the extra class doesn't get generated. It is true that there is no legal reason why a Java compiler can't create temporary files but you expect them to be deleted when the compiler finishes. The class file naming scheme was recently codified into the language definition, so there can be conflicts with "real" anonymous classes. To check that out I added this to the main method: new Object() {public void foo(){System.out.println("foo");}}.foo(); Which produced this result: C:\Temp>del *.class C:\Temp>javac NestedScope.java C:\Temp>dir *.class Volume in drive C has no label. Volume Serial Number is 549D-754B Directory of C:\Temp 08/30/2008 02:42 PM 501 NestedScope$1.class 08/30/2008 02:42 PM 756 NestedScope$Inner.class 08/30/2008 02:42 PM 706 NestedScope.class 3 File(s) 1,963 bytes 0 Dir(s) 3,205,283,840 bytes free C:\Temp>java NestedScope 17 foo C:\Temp> As you can see the pointless NestedScope$1.class was not generated at all (I expected NestedScope$2.class) in this case! What am I missing? I bet it is something obvious. Or is this some sort of "bug"? -Wayne |
|
#9
| |||
| |||
| Sorry, the import statement was left over from some other experiments, and isn't really there. Please remove that 1 line mentally from my post. -Wayne |
|
#10
| |||
| |||
| Użytkownik "Wayne" <nospam@all.4me.invalid> napisał w wiadomości news:48b9964e$0$4912$9a6e19ea@unlimited.newshostin g.com... > I was playing around with inner classes and was > hopping someone could explain the following. Given: [...] > > This works fine when compiled and run. But, I was cleaning > it up when I was done and noticed that javac had created > an extra class file: > [...] > 08/30/2008 02:28 PM 184 NestedScope$1.class > 08/30/2008 02:28 PM 756 NestedScope$Inner.class > 08/30/2008 02:28 PM 671 NestedScope.class > 3 File(s) 1,611 bytes > 0 Dir(s) 3,213,533,184 bytes free > > C:\Temp>javap NestedScope$1 > Compiled from "NestedScope.java" > class NestedScope$1 extends java.lang.Object{ > } > [...] As you can see the pointless NestedScope$1.class was not generated > at all (I expected NestedScope$2.class) in this case! > > What am I missing? I bet it is something obvious. Or is this > some sort of "bug"? > > -Wayne Hi, If you remove 'private' keyword in 'Inner' class, additional method is not generaed. Try to decompile 'NestedClass.class' and 'Inner.class' to get the answer why additional $1 class is created. Przemek |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.