When exactly a static block is loaded in Java ?

Showing Answers 1 - 7 of 7 Answers

suresh

  • Oct 20th, 2005
 

Before creation of object ,static block is loaded  

  Was this answer useful?  Yes

Ramesh

  • Oct 20th, 2005
 

When the class is loaded into the memory first of all the static block is executed.

  Was this answer useful?  Yes

Devidas Sonawane

  • Oct 21st, 2005
 

static block executed before everything. it has no need of object to call. 

  Was this answer useful?  Yes

The code in a static block is loaded/executed only once i.e. when the class is first initialized. A class can have any number of static blocks. Static block is not member of a class, they do not have a return statement and they cannot be called directly. Cannot contain this or super. They are primarily used to initialize static fields.

  Was this answer useful?  Yes

vardhan

  • Nov 29th, 2005
 

when class object created

  Was this answer useful?  Yes

Great Legendary Praveen S Wani

  • Oct 29th, 2006
 

Dear Readers,

To know when exactly static block gets loaded please see the example below

public class Test{

static{

//Do somthing

}

//do some declaration initalisation

//Do some methods

//Do some static methods

}

pulic class StatTest{

//1) when I say Test t = new test ==> static block get executed OR

//2) when I say Test.someStaticMethod(); ==>  static block get executed

//3) suppose the Test Class is extended by some other class and when we create a object of the sub class . the static block gets executed

}

Any body please correct me if I am wrong......

Thanks,

With love

praveen

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions