• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static Questions

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
reply
    Bookmark Topic Watch Topic
  • New Topic