• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initializing the class variables

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..).
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic