I am not clear with inner classes terminology. If we have class Outer and it has inner class Inner, then what do these imply from Inner? (say var is a variable) case 1:var is only defined in Outer case 2:var is only defined in Outer as well as Inner Outer.var Outer.this.var this.var var
If Inner is defined as static/non-static how these work? Thanx in advance
ersin eser
Ranch Hand
Joined: Feb 22, 2001
Posts: 1072
posted
0
you are going to take the test on 29 th. do you have Mr Khalid Mughal's book? it has an entire chapter and an excellent chart that summarizes the inner classes neatly. You are going to need a good understanding of this topic and If you don't have the book and don't want to spend money tryhttp://www.javaranch.com/campfire/StoryInner.jsp or if you have an access just goto local bookstore ( barnes & nobles ) grab a cup of coffee and preferably Khalid Mughal's book read the entire chapter & solve the practice questions. Looks like you need a good review of this topic I would not ignore this chapter and last few days of your studies . you won't regret it. ersin [This message has been edited by ersin eser (edited October 22, 2001).]
leena rane
Ranch Hand
Joined: Aug 13, 2001
Posts: 280
posted
0
Thanx Ersin for the advise, I do not hav Mughal, only RHE,exam cram. Can you answer my question? (BTW if there is a javarancher from Mumbai who can give me the book for 1 day or 15 min-to xerox this chap, please mail me at leena_rane@yahoo.com)
Gagan Indus
Ranch Hand
Joined: Feb 28, 2001
Posts: 346
posted
0
Okay lets talk about following case first : class Outer { int var ; //line#1 class Inner { int var ; //line#2 void method () { // here } } } var at line#2 shadows da var at line#1 . So if we have to refer to var at line#2 , a special syntax of da form : Outer.this.var has to be used .
Now letc wot those 4 variable-access means if placed at line marked //here : 1) Outer.var : A'int valid . Only valid in case in which var at line#1 is declared static . 2) Outer.this.var : For every Inner class instance , there is one Outer class instance associated wid it.This access refers to tht Outer Instance's var , var declared at line#1 dat is . 3) this.var : Refer's to var at line#2 , i guess it is simple rite. 4) var : Same as 3rd , refers to var at line#2
Now comes da second case , when Inner class is declared "static" In this case NO instace of outer class is associated wid any Instance of Inner class . And Inner class can ONLY refer to static variable of Outer class. class Outer { int var ; //line#1 static class Inner { int var ; //line#2 void method () { // here } } }
1) Outer.var : A'int valid . Only valid in case in which var at line#1 is declared static . 2) Outer.this.var : NOT a valid syntax in this case 3) this.var : Refer's to var at line#2 4) var : Same as 3rd , refers to var at line#2
I hope , by now u understand , y da following code prints : 10 20 20 in dat order :
Well i have to agree wid Ersin , Khalid's Inner class chapter is good But even if u r not able to get hold of it , no regrets Just have a look at Inner class chart , in velmurgan's notes . And understand everything it has to offer. Hope someone from mumbai be able to help u out . Do lemme knw if i culd be of help. Good luck for exam
------------------ Gagan (/^_^\) SCJP2 Die-hard JavaMonk -- little Java a day , keeps u going .
Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Gagan... There is an error in the code you have posted.
There is an error in Line A. This will not compile as top level nested classes cannot access non-static members of the enclosing class. [This message has been edited by Shyamsundar Gururaj (edited October 23, 2001).]
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Hi folks... I have been studying Inner Classes from Khalid Mughal's book. Here are a few points that I had summarized for my notes... 1.Top-Level Nested Class (Static) Declaration Context: As a static class member What accessibility can be specified for Class/Interface? ALL Is an outer instance present? NO What is directly accessible in the enclosing context from within the class? Static members in the enclosing class. Can the class define Static or Non-Static members or both? BOTH 2.Non-Static Inner Class Declaration Context: As a non-static class member What accessibility can be specified for Class/Interface? ALL Is an outer instance present? YES What is directly accessible in the enclosing context from within the class? ALL members in the enclosing class. Can the class define Static or Non-Static members or both? ONLY Non-Static 3.Local Class (Static) Declaration Context: In block with a static context What accessibility can be specified for Class/Interface?NONE Is an outer instance present? NO What is directly accessible in the enclosing context from within the class? Static members in the enclosing class + local final variables Can the class define Static or Non-Static members or both? ONLY non-static 4.Local Class (Non-Static) Declaration Context: In block with a non-static context What accessibility can be specified for Class/Interface? NONE Is an outer instance present? YES What is directly accessible in the enclosing context from within the class? ALL members in the enclosing class + Local final variables. Can the class define Static or Non-Static members or both? ONLY non-static 5.Anonymous Class (Static) Declaration Context: As an expression in a static context What accessibility can be specified for Class/Interface? NONE Is an outer instance present? NO What is directly accessible in the enclosing context from within the class? Static members in the enclosing class + Local final variables. Can the class define Static or Non-Static members or both? ONLY non-static 6.Anonymous Class (Non-Static) Declaration Context: As an expression in a non-static context What accessibility can be specified for Class/Interface? NONE Is an outer instance present? YES What is directly accessible in the enclosing context from within the class? ALL members in the enclosing class + Local final variables. Can the class define Static or Non-Static members or both? ONLY non-static 7.Interface Declaration Context: As a package member or a static class member What accessibility can be specified for Class/Interface? PUBLIC ONLY Is an outer instance present? N/A What is directly accessible in the enclosing context from within the class? N/A Can the class define Static or Non-Static members or both? Static variables + non-static method prototypes.
Hope this helps Shyam [This message has been edited by Shyamsundar Gururaj (edited October 23, 2001).]
leena rane
Ranch Hand
Joined: Aug 13, 2001
Posts: 280
posted
0
Thanx Gagan, Thanx Shyamsundar
Gagan Indus
Ranch Hand
Joined: Feb 28, 2001
Posts: 346
posted
0
Oh yes Shyamsundar dat static must be hangover of some ol' program Sorry , the code i intentded to post was :
------------------ Gagan (/^_^\) SCJP2 Die-hard JavaMonk -- little Java a day , keeps u going .