aspose file tools*
The moose likes Beginning Java and the fly likes Initializing the class variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Initializing the class variables" Watch "Initializing the class variables" New topic
Author

Initializing the class variables

Prachi Sathe
Greenhorn

Joined: Oct 30, 2009
Posts: 6
Hi,

What is the best practice to initialize the class variable?

1. At the time of declaring the variable

public class Test
{
private String myPrivateVariable = null;
public String myPublicVariable = null;

public Test
{
// Body of constructor.
......
}

public void functionCalc()
{
myPrivateVariable = getPrivateString(); // Some function returning String value
myPublicVariable = getPublicString();// Some function returning String value
}
...
}

2. In Constructor

public class Test
{
private String myPrivateVariable;
public String myPublicVariable;

public Test
{
// Body of constructor.
myPrivateVariable = null;
myPublicVariable = null;
......
}

public void functionCalc()
{
myPrivateVariable = getPrivateString(); // Some function returning String value
myPublicVariable = getPublicString();// Some function returning String value
}
...
}

3. First use in the code

public class Test
{
private String myPrivateVariable;
public String myPublicVariable;

public Test
{
// Body of constructor.
......
}

public void functionCalc()
{
myPrivateVariable = getPrivateString(); // Some function returning String value
myPublicVariable = getPublicString();// Some function returning String value
}
...
}



Thank you,
Prachi
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

Hi Prachi,
Welcome to JavaRanch . Next time you post the code please use code tags. And by "class variables" you mean the instance variables, usually static variables are called class variables that they are part of the class not on a particular instance. For the instance variables compiler gives the default value if you have not specified any (object references -> null, int -> 0 etc..).


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38405
    
  23
As you have been told, the variables you posted are not usually called "class variables". The answer depends on what you intend to do with these variables. Are they supposed to have the same initial value in every instance of this class? In that case you can initialise them at declaration time. Are they supposed to have different values for different instances? In that case they are probably best initialised in the constructor.

My own preference would be for all instance variables to be initialised in the constructor; then you have all your initialisations together in one place and can see them all.

Your classes quoted show some serious design flaws: you ought not to allow reference type fields to be null; they might be used before they are initialised to a real value. Even worse, you have a public instance field; you are binding your class to a particular implementation by using public fields. And also permitting other code to change this value outwith the control of your class. If you have method which return those two Strings, then is there any need for your fields at all? Can you dispense with those fields and simply use the two methods?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Initializing the class variables