Get your CodeRanch badge!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner class - nested Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner class - nested" Watch "inner class - nested" New topic
Author

inner class - nested

Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
Hi all
I was testing thru the inner classes and got some insane idea to code this. I need some explanations.


(1) When the TestInner does NOT extend the TestSI class, then TestInner.modify() works. But after 'extends', it doesn't. I am at loss to explain this.
(2) Can somebody please explain the behaviour in this case?
Thnx in advance.
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375

(1) When the TestInner does NOT extend the TestSI class, then TestInner.modify() works. But after 'extends', it doesn't. I am at loss to explain this.

I tried it with & without extends in your code as-is . It works either ways . If it is something specific plz let us konw.
The following is for the line marked (2)
InnerMost is defined static & to invoke a static member of a class we need a reference of the said class . Since TestInner is also of type TestSI the following invocation is valid
new TestSI.TestInner.InnerMost(t)
If TestInner were not to extend TestSI then TestInner would not be a refernce of type TestSI & hence the above declaration would become invalid .
I maybe wrong so feel free to correct me.
Ashish H.
[ August 20, 2002: Message edited by: Ashish Hareet ]
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
Hi Ashish
Thanks for the reply. What you said about the static Inner class (2) makes a little sense, but I haven't totally understood how it worked.
- I created another class 'InnerMost' inside TestInner too - after making TestInner also static.
- So now we have TWO Inner classes with name, InnerMost.
- Now, the line (2) is looking for the 'TestSI.TestInner.InnerMost' class, which makes sense.
So how did the line in the previous code get resolved for the code to get compiled?
Regarding (1), This is the code i used.

Output with 'extends Test' commented out -
AAA 10
AAA 10
AAA 20
Output with 'extends Test' uncommented -
AAA 10
DEFAULT -99
AAA 10
Should have given the same output - if not because it is an inner class, but for the reason that it is extending atleast. I'm really confused.
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Hi Vin,
This is regarding line (2)
Here's what I did,
1.Made TestInner static
2.Created another class InnerMost inside TestInner & declared InnerMost as static
The result is that
new TestSI.TestInner.InnerMost(t);
now calls the new class we created
Reason -
TestSI.TestInner is a reference to the static TestInner class inside TestSI. The compiler will now look for a class InnerMost in TestInner which is how
new TestSI.TestInner.InnerMost(t);
is resolved.
Now consider the code from your first post -
TestSI.TestInner is a reference of type TestInner & also TestSI.When we say
new TestSI.TestInner.InnerMost(t);
the compiler will look for the class InnerMost in TestInner.It does not find any such class so it looks for InnerMost in the parent of TestInner which it does find & that is how I think it is resolved.
Now for the new code you've furnished -
I'll assume that you understand the output when "extends Test" is commented out.
If you let the "extends Test" be there the confusion is with the line
ti.modify();
which results in "DEFAULT -99"
When ti is created , since it extends Test we need to call the constructor of Test. We do not explicitly call the Test constructor with the String argument & so the default constructor is called which initializes the name variable to DEFAULT & i to -99. Now when we call the modify method of TestInner what should it use in place of name & i . Should it use the relevant variables it gets from it's enclosing instance or should it use the relavant variables it has in it's own instance ( i.e. the variables it inherits from it's parent ) ? The answer is that it uses the variables from it's own instance which gives you the confusing output.
Try this -
Inside the modify method in TestInner change the lines to the following -
System.out.println(Test.this.name + " " + Test.this.i);
Test.this.i *= 2;
You will get the same output ( as in the case when "extends Test" is commented out ) because now you are using the variables from the enclosing instance.
HTH
Ashish H.
You wrote some real interesting peice of code
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
Hi Ashish
Thanks a lot for the detailed explanation. It was excellent. I never bothered to think that the enclosing class's variables would be hidden by the instance variables.
Regarding line (2), the output did indicate that the JVM behaved in the way you explained. I guess i have to look up the memory structure and dynamic binding implementation of java classes.
Do you know of any books that describe the memory layout of java objects? Is it similar to c++'s virtual table and virtual pointers?
Hey, congrats on the 200th post.
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Hi Vin,
The only good book that can help you with the intricate details of Java IMHO is the JLS ( & yes Thinking in Java ). JLS maybe cryptic but it sure is the tops.
Hey, congrats on the 200th post.

200 posts in 1 year - Well thanx anyway
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inner class - nested
 
Similar Threads
Doubt about Array
Threads question
Inner class
simple super & this question
Inner class