WeiJie Lim wrote:
I thought in Java, we must always declare the variables before using them ?
Why in this case, the helloMessage variable can be used before being declared ?
hi weijie.
in java LOCAL VARIABLES should be initialized before using them . for e.g
in your case helloMessage is NOT a local variable. it is a CLASS variable. in case of class variables and instance variables they are given default values , if you don't initialize them. the default values are as follows :
int - 0
float -0.0f
boolean - false
Any Object type - null
Since
string is of type Object, it is initialized to null by default.