Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static members initialization and static object references

 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 queries here:

1)I am confusing with the static members initialization in a program that my friend has given me to trace.
I know execution starts from main.
In main() I have some static objects created by new() like
static MyClass foo=new MyClass();
This statement is executing first even though it is the last statement in[/ main(). :(
Before this statement I have some System.out.print() statements and other code.
I felt nervous and afraid.Should I review strongly the order of initialization and execution in main().

2) What is the need of attaching a static modifier to an object reference.
Will it be a compile time error if I don't attach that static modifier before object reference in main() ?
 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In main() I have some static objects created by new() like
static MyClass foo=new MyClass();

This never ever will compile...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vijayk gopu:
...In main() I have some static objects created by new() like
static MyClass foo=new MyClass();
This statement is executing first even though it is the last statement in[/ main().
Before this statement I have some System.out.print() statements and other code...



Could you post your code?
 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Marc,

Please find the code:
There are 4 classes.One is Bowl, second is Table , 3rd is CupBoard.
A test class StaticInitialization.

I need clarification about execution in main().

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is from the Static data initialization section in Bruce Eckel's Thinking in Java. (Always appropriate to cite sources.)

Note that the lines...

static Table t2 = new Table();
static Cupboard t3 = new Cupboard();

...are NOT contained in the main method. They are after the closing brace of main's body. These lines are simply static variables in the class StaticInitialization.

Indeed, the whole point of this code is to show that static variables are initialized when the class is initialized, which happens before the body of main is executed.
 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing "static" in main()...

Rearrange the code a bit and it will be more clear i think.


Edit: Needed too much time to reply...
[ January 21, 2007: Message edited by: Anton Uwe ]
 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc and Anton, thanks for your replies.
May be it's the time I must consult an eye specialist!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic