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