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 Very Simple Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Very Simple Question" Watch "Very Simple Question" New topic
Author

Very Simple Question

Umair Uddin
Greenhorn

Joined: Sep 12, 2001
Posts: 14
class Test {
Test(int i) {
{
System.out.println("TEST("+i+")");
}

public class Question {
static Test t1=new Test(1);
Test t2=new Test(2);
static Test t3=new Test(3);
public static void main(String args[]) {
Question Q=new Question();
}

}
I have a question its easy but i have some confusion to understand logic..
please dicuss it - when we are going to discuss the sequnce of the above program so we find it as:::::
1)JVM goes to main method and make the object of Question Class..
2)JVM goes to Question class and make Object of nonstatic Test(instance)
3)JVM goes into Test Class and read constructor pass arguments in parameter list and print message..
my basic question is that why in static case JVM not going into Test class ?? and in nonstatic case JVM going into Test Class ??? note: when Object of Question class is exist at the same time JVM prints 1 and 3 ...
Thankx alot ...
waiting for reply
Umair Uddin Qa dd'rouy
mitchner green
Greenhorn

Joined: Sep 18, 2001
Posts: 21
Are you saying the code you gave actually compiles? I got a slew of compiler errors when I tried it.
John Page
Greenhorn

Joined: Sep 26, 2001
Posts: 2
I adjusted your post a little and ran the following code:

It ran fine.
However the result was very interesting. It first created the static Test objects and THEN created the non-static object t2.
The output was :
TEST(1)
TEST(3)
TEST(2)
Interesting, but not suprising.
Rewriting the Question class illustrates what occurs:

Now the output is:
TEST(1)
TEST(3)
Static references exist for all instances of a particular class. (Such as Question) As soon as the class is entered, all the static references be they primitives or Objects must be instantiated and made available. The instance reference to t2 is not instantiated until the second half of the line:
...new Question();
the "new" keyword instantiates a particular instance of Question.
Try this one out:

Now the output is:
TEST(1)
TEST(3)
1
You see, there is no reference to any particular instance of Question. Not even a "null" one. As soon as the JVM enters the class it makes all the static references available.
This is a great question, that touches on some subtle issues.
I hope my reply helps.
John
[This message has been edited by John Page (edited September 27, 2001).]
[This message has been edited by John Page (edited September 27, 2001).]
John Page
Greenhorn

Joined: Sep 26, 2001
Posts: 2
Actually, this version illustrates when the static references to Test are instantiated, best of all:

Output:
TEST(1)
TEST(3)
The result speaks for itself.
John
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Very Simple Question