So if i have something like this:
public class TestClass {
public static void main(String[] args) {
String hello;
System.out.println(hello);
}
}
So this would obviously not work. But considering i have such a situation in a program, how can i catch such exception, can you please tell.
Thank you,
EDIT:
So if i have something like this:
class ABC{
public static String myString;
public ABC(String myString){
myString = this.myString;
}
public static String getString(){
return myString;
}
}
and now if in another class, without calling the constructor of ABC i do like:
ABC.getString();
This is what i am trying to say… you get it??
As others pointed out, the first case won’t compile.
You should assign a value to the String.
nullor an empty string are the usual default values.Consider carefully what would be a logical default value.
Better yet, consider why the code would encounter an uninitialized variable, determine what the value the variable should have (whether a default value or not), and modify your code accordingly.