• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initializing the class variables

 
Prachi Sathe
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..).
 
Campbell Ritchie
Sheriff
Pie
Posts: 49805
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic