• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

<static inner class >issue

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys, do static inner classes get precedence over outerclasses ??

 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

LANGUAGES in general are really a world of amazement ..arent they ?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is some outstanding R&D that you did there, Punit! And yes, this questions involving initialization are tricky.
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic