File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes static members initialization and static object references Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static members initialization and static object references" Watch "static members initialization and static object references" New topic
Author

static members initialization and static object references

vijay kumarg
Ranch Hand

Joined: Dec 14, 2006
Posts: 105
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

Joined: Jan 10, 2007
Posts: 122
In main() I have some static objects created by new() like
static MyClass foo=new MyClass();

This never ever will compile...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
vijay kumarg
Ranch Hand

Joined: Dec 14, 2006
Posts: 105
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Jan 10, 2007
Posts: 122
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

Joined: Dec 14, 2006
Posts: 105
Marc and Anton, thanks for your replies.
May be it's the time I must consult an eye specialist!
 
 
subject: static members initialization and static object references
 
Similar Threads
it's about "Argument Passing"
Static reference for object
About instance in. and static initializer
Mughal, review question 9.6, p.282 (Synchronization)
Hi