Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Static variable initialization

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..

 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic