1

This is the source code for demonstrating a static block and static method.

class StaticDemo
{
static int count=10;

 StaticDemo(){
 count++;
 }
 static void display(){
  System.out.println(count);
 }

 static{
  System.out.println("Static Block written Before Main");
 }

 public static void main(String args[]){
     StaticDemo t1=new StaticDemo();
     StaticDemo t2=new StaticDemo();

     StaticDemo.display();
     t2.display();

  }
  static{
     System.out.println("Static Block written After Main");
  }
 }

I expect an output of

Static Block written Before Main 
Static Block written After Main 
11
12

whereas I get an output of

Static Block written Before Main 
Static Block written After Main 
12
12

I understand that the static blocks are the first set of code to be executed and hence the order of the first 2 statements. However, when the JVM creates the object t1 of StaticDemo class, doesn't the value of count get incremented to 11 ( 10+1 )? When does the initialization/increment actually occur in the program? Thank you!

1
  • What do you expect? It works just as should. Commented Apr 24, 2013 at 11:19

6 Answers 6

4

The increment occurs in the constructor. You call the constructor twice before displaying the counter so it is incremented twice. If you place the display() between the two constructor calls you will see 11 12 as you expect.

BTW You can see all this by stepping through the code in your debugger.

Sign up to request clarification or add additional context in comments.

Comments

3

The output is explained by the order of the statements in your main method:

 StaticDemo t1=new StaticDemo();       //this increments count to 11
 StaticDemo t2=new StaticDemo();       //this increments count to 12

 StaticDemo.display();                 //this outputs 12
 t2.display();                         //this outputs 12

Essentially, you're incrementing twice, then outputting twice - hence the result.

Comments

2

Look at your constructor -

StaticDemo(){
    count++;
}

You are incrementing the static variable every time you create an object. You've created two objects here, so it will get incremented by two. This explains why you are getting 12 two times.

To get your "expected" output -

public static void main(String args[]){
    StaticDemo t1=new StaticDemo();    // count got incremented, now contains 11
    StaticDemo.display();    // will print 11

    StaticDemo t2=new StaticDemo();  // again count got incremented
    t2.display();    // will print 12
}

I hope this clears up the actual output for you.

Comments

1
 public static void main(String args[]){
     StaticDemo t1=new StaticDemo();
     StaticDemo.display();
     StaticDemo t2=new StaticDemo();
     t2.display();

  }

Output:

Static Block written Before Main Static Block written After Main 11 12

edit:// problem is you call display(), after you called the constructer 2 times. because display() using the same value you get each time 12

edit2:// to late sry

Comments

0

See below:

public static void main(String args[]){
             //Initially value of count is 10
             StaticDemo t1=new StaticDemo(); //here it becomes 11
             StaticDemo t2=new StaticDemo(); //here it becomes 12

             StaticDemo.display(); //Now you are printing, it will print 12
             t2.display(); //The value has not changed, so again 12

          }

If you re-write the block as:

 public static void main(String args[]){

             StaticDemo t1=new StaticDemo(); 
             StaticDemo.display(); 
             StaticDemo t2=new StaticDemo(); 
              t2.display(); 

          }

Then you should see the output as 11, 12

Comments

0

@user2315188..

Your are creating t1 and t2 objects so the no-argument constructor call 2 times,the first time count values is 11 and second time the count become 12 so you are getting output 12,12

     StaticDemo t1=new StaticDemo();
     StaticDemo t2=new StaticDemo();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.