Below is my code, wondering why null is printed instead of Hello World!!!
public class MyClass {
static String s1 = getMyValue();
static String s2 = "Hello World!!!";
private static String getMyValue() {
return s2;
}
public static void main(String args[]) {
System.out.print(s1); //outputs: null
}
}
s1gets assigned togetMyValue()which returnsnullsinces2hasn't been assigned yet.this.getMyValue()seemed like a common usage in C#, PHP and as far as I recall Java. May be something has changed. Anyone can shed some light onto this? Actually, class properties to be assigned using another method would usually be performed within a constructor.