Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static variable initialization

 
Phani Raju
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 sinha
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic