aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static" Watch "static" New topic
Author

static

rex tony
Ranch Hand

Joined: Aug 29, 2007
Posts: 159
hi,
What is order during loading
1. static variable
2. Static Block
3. Static method
Kishore Kumar
Ranch Hand

Joined: Oct 15, 2007
Posts: 71
Hi Rex,
From top to bottom of the class you write, which ever comes first.

class Static
{
static int a = fun();

static{
System.out.println("inside static block");
}

static int fun(){
System.out.println("inside static method");
return 1;
}
public static void main(String[] args)
{

}
}

In this case answer is
inside static method
inside static block


class Static
{

static int fun(){
System.out.println("inside static method");
return 1;
}

static{
System.out.println("inside static block");
}
static int a = fun();
public static void main(String[] args)
{

}
}



In this case answer is
inside static block
inside static method
Doug Slattery
Ranch Hand

Joined: Sep 15, 2007
Posts: 294
In this case answer is
inside static block
inside static method


This is incorrect.

Because the variable a is being assigned the return value of foo, foo will run before the static block so the output will always be:

inside static method
inside static block

regardless of which comes first in the code. If you assign the variable a something else (like a constant: static int a = 99;), then foo never runs at all and the out put is:

inside static block

Static blocks will run when the class is loaded and before the blocks class' constructor (and its superclass constructors) run. Non-static blocks run after the class' superclass constructors run but before the class' constructor runs.

Whatever happened to keep it simple .

Aloha,
Doug

-- Nothing is impossible if I'mPossible
Kishore Kumar
Ranch Hand

Joined: Oct 15, 2007
Posts: 71
Hi Doug Slattery ,
----------------------------------------------------------------------------
class Static
{

static int fun(){
System.out.println("inside static method");
return 1;
}

static{
System.out.println("inside static block");
}
static int a = fun();
public static void main(String[] args)
{

}
}

In this case answer is
inside static block
inside static method

----------------------------------------------------------------------------

But when i complied, i got the answer as what i have mentioned.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Doug's response is incorrect. As Kesava mentioned, inline initializers will be executed in the order in which they appear in the code, interleaved with any initialization blocks that may also be present.


SCJP 5.0
Doug Slattery
Ranch Hand

Joined: Sep 15, 2007
Posts: 294
Doug's response is incorrect.

Ok, my bad, in part. I missed moving the variable a below the static block on the second example after fiddling with something for my own clarification.

Now, if you change the declared variable from:


to:

then the only output is
inside static block
because the static method fun() is never called.

Kelvin is correct in saying static blocks will execute in the order they appear, but in this example, there is only one static block.

Aloha,
Doug

-- Nothing is impossible if I'mPossible
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static