File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

<static inner class >issue

 
Abhishek Bhat
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, do static inner classes get precedence over outerclasses ??

 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

LANGUAGES in general are really a world of amazement ..arent they ?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is some outstanding R&D that you did there, Punit! And yes, this questions involving initialization are tricky.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic