wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static Questions" Watch "Static Questions" New topic
Author

Static Questions

Karen Leoh
Ranch Hand

Joined: Dec 03, 2001
Posts: 40
Hi there.
I come across this question posted by Anil Paul in JavaDiscuss, I hope someone can help me out. Thanks.
Question: What is the output?
public class StaticTest
{
static
{
System.out.println("In static");
}
{
System.out.println("In non - static");
}
public static void main(String args[ ])
{
StaticTest st1;
System.out.println(" 1 ");
st1 = new StaticTest();
System.out.println(" 2 ");
StaticTest st2 = new StaticTest();
}
}
Answer:
In static
1
In non - static
2
In non - static

--------------------<p>Karen Leoh<br />Sun Certified Programmer for Java™ 2 Platform
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Hi there.
I come across this question posted by Anil Paul in JavaDiscuss, I hope someone can help me out. Thanks.
Question: What is the output?
public class StaticTest
{
static
{
System.out.println("In static");
}
{
System.out.println("In non - static");
}
public static void main(String args[ ])
{
StaticTest st1;
System.out.println(" 1 ");
st1 = new StaticTest();
System.out.println(" 2 ");
StaticTest st2 = new StaticTest();
}
}
Answer:
In static
Here "StaticTest st1;", this load the class. So the static {...} get to run.

1

Here, just System.output....

In non - static
Here "st1 = new StaticTest();" The Object actually created. So you are in the none static {...}. It is like a no name constructor but it get called before constructors.
2
Just another system output.
In non - static
Again, another Object got created. So it calls the {}. Just like above

Karen Leoh
Ranch Hand

Joined: Dec 03, 2001
Posts: 40
Thanks so much Fei Ng. Your explanation is clear.Just let me rephase what I understand.
When a reference variable (st1) is declared, it will invoke the static method but when an object is created using the new keyword the non-static method is invoked.
Correct me if I am wrong.
Thanks.
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Originally posted by FEI NG:
Hi there.
I come across this question posted by Anil Paul in JavaDiscuss, I hope someone can help me out. Thanks.
Question: What is the output?
public class StaticTest
{
static
{
System.out.println("In static");
}
{
System.out.println("In non - static");
}
public static void main(String args[ ])
{
StaticTest st1;
System.out.println(" 1 ");
st1 = new StaticTest();
System.out.println(" 2 ");
StaticTest st2 = new StaticTest();
}
}
Answer:
In static
Here "StaticTest st1;", this load the class. So the static {...} get to run.
Actually, the static{ . . . } runs when you type java StaticTest at the command prompt. If you comment out the declarations and initializations, In static will still be written. -- MP

1

Here, just System.output....

In non - static
Here "st1 = new StaticTest();" The Object actually created. So you are in the none static {...}. It is like a no name constructor but it get called before constructors.
2
Just another system output.
In non - static
Again, another Object got created. So it calls the {}. Just like above

Great explanation. I hope you don't mind me adding a correction in your quote. It seemed like the best place for it. It is in blue.
Matthew Phillips

[This message has been edited by Matthew Phillips (edited December 14, 2001).]


Matthew Phillips
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Questions
 
Similar Threads
When is a class loaded?
Static
Static Block
static
Just a doubt on static methods