This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static block,non-static block, & constructor issue

 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above code:
First the static block will run: (0)(3)
Then the main method will run : (3)
Then the constructor will run : (3,0)

Then whether non-static block
will run or main method's q.i
and q.j will run???


Suppose if main methods q.i &
q.j runs first, then : (3,0)

Then non-static block : (3,0)(3,2)
--------------------------------------------------------------
so final answer for me:[B](0)(3)(3)(3,0)(3,0)(3,0)(3,2)

Even if the non-static block runs before the main methods q.i and q.j, the output should be: (0)(3)(3)(3,0)(3,0)(3,2)(3,2)

But the output of the pgm is (0)(3)(3)(3,0)(3,2)(3,2)(3,2) how???
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Henryson:


In the above code:
First the static block will run: (0)(3)
Then the main method will run : (3)
Then the constructor will run : (3,0)

Then whether non-static block
will run or main method's q.i
and q.j will run???


Suppose if main methods q.i &
q.j runs first, then : (3,0)

Then non-static block : (3,0)(3,2)
--------------------------------------------------------------
so final answer for me:[B](0)(3)(3)(3,0)(3,0)(3,0)(3,2)

Even if the non-static block runs before the main methods q.i and q.j, the output should be: (0)(3)(3)(3,0)(3,0)(3,2)(3,2)

But the output of the pgm is (0)(3)(3)(3,0)(3,2)(3,2)(3,2) how???


1>>>first, static block is executed giving:
(0)(3)
2>>>then, 'System.out.print("("+i+")");' giving:
(3)
3>>>then, the non-static initialization block
{
System.out.print("("+i+","+j+")");
j = 2;
System.out.print("("+i+","+j+")");
}
giving:
(3,0)(3,2)
4>>>then, the constructor method is executed: giving:
(3,2)
5>>>then 'System.out.print("("+q.i+","+q.j+")");' giving:
(3,2)
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you try to use static stuff, then java makes sure it is there, the static things, without the object itself,... so it does the static initializations. after then, it executes your static requests.

the when you try to call the constructor, it first executes non-static blocks, then calls the constructor
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This much clearer than testing with all those variables.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic