• 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 v/s Non-Static

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output of the following code?

A) Test(1)
Test(2)
Test(3)

B) Test(3)
Test(2)
Test(1)

C) Test(2)
Test(1)
Test(3)

D) Test(1)
Test(3)
Test(2)

Answer : D
Explanation : No matter where they are declared, static variables will be initialized before non-static variables

I m still confused about the printing order...In the first call to the constructor why wouldnt Test(2) print before Test(3) even if 2 is initialized later?

[ August 09, 2004: Message edited by: Murtuza Akhtari ]
[ August 09, 2004: Message edited by: Murtuza Akhtari ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static variables are declared and initialized when the class gets loaded at runtime. Since they are not tied to a specific instance of a class, their declaration and initialization does not wait for an instance of a class to be created. t1 and t3 are declared and initialized when the class is loaded, so the Test construtor is called twice separately and prints Test1, Test3.
main() is executed at runtime after the class has been loaded. In main() when an instance of class Q12 is created, its instance variable t2 is declared and initialized. The Test construcor is called and prints Test2.
 
Murtuza Akhtari
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Aarti !!
 
Enjoy the full beauty of the english language. Embedded in this 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