my dog learned polymorphism*
The moose likes Beginning Java and the fly likes static block,non-static block, & constructor issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static block,non-static block, & constructor issue" Watch "static block,non-static block, & constructor issue" New topic
Author

static block,non-static block, & constructor issue

Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200


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

Joined: Feb 26, 2005
Posts: 2049
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

Joined: Feb 26, 2005
Posts: 2049
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

Joined: Oct 27, 2004
Posts: 198
This much clearer than testing with all those variables.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static block,non-static block, & constructor issue
 
Similar Threads
Val's Exam Question numer 54
enums do not allow access to static members within constructors and instance init blocks
language fundementals
Doubt with static block+variable