File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static block,non-static block, & constructor issue

 
Mark Henryson
Ranch Hand
Posts: 200
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This much clearer than testing with all those variables.

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic