Static block  java

Squadless
Squadless used Ask the Experts™
on
I wrote up this little scenario.  What would be the sequence of the methods called.  

I think the order will be:
1) static block,
2) the constructor
3) constructor of Blah.

The 3rd is trivial, but are 1 and 2 in correct order?  - This is granted that another class would call the "method" function of TestClass.

Thanks.
public class TestClass extends Blah{

TestClass(){
  super();
}

public void method(){
 TestClass baa = new TestClass();
}

static blockOfCode{

}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Awarded 2011
Awarded 2011

Commented:
I think this is correct.
I think static block will be executed when this class will be loaded.

But then for some third party object ro call the method() it will first need to
create instance of TestClass - then it would first have to call the constructor.

You probably meant to  make the method() static method ?
Awarded 2011
Awarded 2011

Commented:
static block will be excuted when the class is loaded

But constructor will only be executed when someone creates instance or calls method.

And the superclass constructor will be called from executing constructor as written

Author

Commented:
Lets take 2 scenarios.  
One - method is NOT static and when a 3rd party object does create a instance of test class then calls instanceVar.method();
Two - method IS static when a 3rd party object invokes TestClass.method();

How would that differ?  If method is static, when calling TestClass.method(), we are directly going to the static method class... yes right , we're not creating a instance of TestClass so the static block is not yet executed.  So IF and only IF we create a regular instance of a class, static blocks execute first.  

So What if i had
final int value = 7; above the constructor
final int valueB = 8; INSIDE the constructor
final int valueC = 9; INSIDE the function "method".

Would any of the finals initialize before the static block?
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Mick BarryJava Developer
Top Expert 2010

Commented:
>  So IF and only IF we create a regular instance of a class, static blocks execute first.  

No the static block will be called first in both scenarios (assuming the class has not already been loaded)

Mick BarryJava Developer
Top Expert 2010

Commented:
> Would any of the finals initialize before the static block?

no
Awarded 2011
Awarded 2011
Commented:
No I think stsic block executes when you load the class, so TestClass.method() will still execut e static block
Java Developer
Top Expert 2010
Commented:
> I think the order will be:
> 1) static block,
> 2) the constructor
> 3) constructor of Blah.

Its actually 1, then 3, then 2

You cannot execute the constructor until the base class has been constructed

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial