• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static/Non Static

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21497
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic