the java documentation mentioned
the static modifier, in combination final modifier, used define constants.
i wondering, can final keyword alone used define constant in java. know variable declared final keyword , without static keyword can't changed, count constant?
you need static keyword eliminate context of class instance. take @ following code:
public class main { public static final string const; static { if((system.currenttimemillis() % 2) == 0) { const = "foo"; } else { const = "bar"; } } public final string const2; public main(){ if((system.currenttimemillis() % 2) == 0) { const2 = "foo"; } else { const2 = "bar"; } }
}
while creating multiple instances of main result in different values const2, const initialized when class loaded , stays same on several instances of main.
the more interesting may have static final variable different values in case multiple classloaders involved.
so, constant in java final static variable initialized constant value.
Comments
Post a Comment