This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static/Non Static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static/Non Static" Watch "Static/Non Static" New topic
Author

Static/Non Static

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
When are static members initialised and when are non static member intialised.
i know non static members are intialised with the creation of object and static when the class is loaded.
But doesnt that happen one after the another.
if you have



Does the order in which you declare static and nonstatic members make a difference?
Can you create and object with out decaring it.
ie


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

i know non static members are intialised with the creation of object and static when the class is loaded.
But doesnt that happen one after the another.


Yea... for the first one. But what about the second? Or the third? etc. The class is loaded once, but tons of objects can be created, of that class. (This can be further complicated with class loaders, but let's not go there)

Does the order in which you declare static and nonstatic members make a difference?


Yes... Static variables are initialized from the top of the class to the bottom (as they are declared in source) -- during class loading. Same goes for instance variables during construction.

Can you create and object with out decaring it.


Declarations are for variables. You can create an object without assigning it to a variable. As you've shown in code.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static/Non Static
 
Similar Threads
Hi can anybody solve this problem....
Instantiate a generic parameter type
static object refrences and non static object refrences
Static variables and methods
asking about interfaces