Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static

 
rex tony
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
What is order during loading
1. static variable
2. Static Block
3. Static method
 
Kishore Kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Doug Slattery
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic