This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes <static inner class >issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "<static inner class >issue" Watch "<static inner class >issue" New topic
Author

<static inner class >issue

Abhishek Bhat
Greenhorn

Joined: Jan 01, 2009
Posts: 25
Guys, do static inner classes get precedence over outerclasses ??

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Its all depending on the position of your inner class instance creating statement. Just put that statement after instance initializer block, output will change or just delete Animal1 a2=new Animal1(); output will change.



SCJP 6
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


Here
1) static Animal a = new Animal("first static"); is called.
this calls constructor

2) before executing first line of constructor, instance variable initialization and initialization block is called from constructor.
So now constructor calls


line1 is instance variable and line2 is instance initializer block.
If you change sequence of these output will change.

3)Animal1 a2=new Animal1(); calls


So "Hi this is static" is executed first.

4)
then this prints "Hello "+nothing

5) static Animal a1 = new Animal("second static"); is called, same thing happens for this also but

is not executed as it is static.

6) then again
//line2
{
System.out.println("Hello "+name);
}
is executed.

7) now third static is called.


8. ) After that main()'s first statement is executed.



Now if you make Animal1 a2=new Animal1(); static.


output will be :
Hello nothing
wats up?: first static
Hello nothing
wats up?: second static
Hi this is static
Hi this is static1
Main start...
Hello nothing
wats up?: Main


Means statics are executed now line by line.
Abhishek Bhat
Greenhorn

Joined: Jan 01, 2009
Posts: 25

Hi Punit,

Thanks a ton. i put up a dumb show ... i did not play with it.....And it wont help in my preps....thanks a million once again to you buddy...

I got it.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
It was really a tricky question Abhishek, I have to do some R&D with the code to clearly understand the flow.
Abhishek Bhat
Greenhorn

Joined: Jan 01, 2009
Posts: 25

LANGUAGES in general are really a world of amazement ..arent they ?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
That is some outstanding R&D that you did there, Punit! And yes, this questions involving initialization are tricky.


All code in my posts, unless a source is explicitly mentioned, is my own.
 
 
subject: <static inner class >issue
 
Similar Threads
Accessing static methods and variables
Overriding Vs Redefinition
Runtime "Exception in thread "main" '
Regarding reference variable
super() ever have arguments?