0

Inside the class MyClass, I thought this code would return a compiler error, because final modifer preceding myString means that the reference to the object cannot be changed (in this case, the value of reference would be NULL since it hasn't been defined yet until I instantiate it, unless I'm misunderstanding this process), but the code compiles without an error, and print "Hello World". So why does it compile?

final String myString;

public MyClass(){
    myString = "Hello World";
}
public static void main(String args[]) {
    MyClass x = new MyClass();
    System.out.println(x.myString);
}
Ted
  • 469
  • 4
  • 16

0 Answers0