aspose file tools*
The moose likes Java in General and the fly likes Static variable initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Static variable initialization" Watch "Static variable initialization" New topic
Author

Static variable initialization

Phani Raju
Greenhorn

Joined: Aug 03, 2007
Posts: 19
Hi

My understanding is that all static variables and static blocks shall be initialized/executed before anyone accesses the class (static method or object creation). Below code is not working as expected, please help me in understanding where I am wrong.

public class StaticTest
{
private static StaticTest myInstance = new StaticTest();
static String str = null;
static int i =0;
private StaticTest()
{
try
{
i = 1;
str = "constructor";
}
catch(Exception e){}
}
public static String getData()
{
return i + str;
}
}

Test class:

public class Test
{
public static void main(String[] args)
{
System.out.println(StaticTest.getData());
}
}

Output: "0null"

I was expecting: "1constructor"
Awishek sinha
Ranch Hand

Joined: Jun 24, 2007
Posts: 62
Originally posted by Phani Raju:
Hi

My understanding is that all static variables and static blocks shall be initialized/executed before anyone accesses the class (static method or object creation). Below code is not working as expected, please help me in understanding where I am wrong.

public class StaticTest
{
private static StaticTest myInstance = new StaticTest();
static String str = null;
static int i =0;
private StaticTest()
{
try
{
i = 1;
str = "constructor";
}
catch(Exception e){}
}
public static String getData()
{
return i + str;
}
}

Test class:

public class Test
{
public static void main(String[] args)
{
System.out.println(StaticTest.getData());
}
}

Output: "0null"

I was expecting: "1constructor"



I believe if you modify the getData() method of class StaticTest
so that it return
return myInstance.i + myInstance.str;

You would get the value as expected


Awishek Kr Sinha<br />SCJP 1.4<br />SCBCD 1.3
Awishek sinha
Ranch Hand

Joined: Jun 24, 2007
Posts: 62
This is because when you call the method StaticTest.getData()
(as the contructor is private) only one object get's created and its reference is assigned to myinstance,as the reintilization of variable happens in constructor as have to have to use the refence "myInstance" to see what you are expecting
Phani Raju
Greenhorn

Joined: Aug 03, 2007
Posts: 19
Awishek, thanks for responding.

It didn't work and I think accessing the variables as "myInstance.i" won't or rather shouldn't make any difference as they are class variables.
Phani Raju
Greenhorn

Joined: Aug 03, 2007
Posts: 19
All static blocks of a class are getting executed before accessing any static method of the class. Below are few rules regarding static blocks I am aware of:

1. Static blocks are guaranteed to be executed before any type of access to the class
2. Only runtime exceptions are allowed in these blocks of code
3. The order of execution (in case of more than one static block) is the textual order

Can someone please list out the rules for initialization of static variables...
Awishek sinha
Ranch Hand

Joined: Jun 24, 2007
Posts: 62
make the static creation of object after the primitive declartion

static String str = null;
static int i =0;
private static StaticTest myInstance = new StaticTest();
as intially the object myInstance got created and its reassigned in the contructor to 1 & constructor but after that in satic declartion it gain got reset to o & null
I hope this helps..
Phani Raju
Greenhorn

Joined: Aug 03, 2007
Posts: 19
oops...I realized my mistake, as I was explicitly initializing the static variables (though to their default values), they were not getting overridden.

Corrected my mistake and is working as expected:

public class StaticTest
{
private static StaticTest myInstance = new StaticTest();
static String str;// = null;
static int i;// =0;
private StaticTest()
{
try
{
i = 1;
str = "constructor";
}
catch(Exception e){}
}

public static String getData()
{
return i + str;
}
}

Thanks
Sahid Khan
Ranch Hand

Joined: Jun 27, 2007
Posts: 41
oops...I realized my mistake, as I was explicitly initializing the static variables (though to their default values), they were not getting overridden.


Actually they were getting overwritten by variable initializer.

is in some sense similar to


So what happens, the values modified in the constructor get rewritten by initializer which comes after the invocation of the constructor.

Another work around can be calling the constructor after the variable initializer. Like this..

Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Sahid Khan:
So what happens, the values modified in the constructor get rewritten by initializer which comes after the invocation of the constructor.

Another work around can be calling the constructor after the variable initializer.


Thats a good observation Sahid Khan.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
jQuery in Action, 2nd edition
 
subject: Static variable initialization