Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Dieter Quickfend wrote:I'm having a problem with this. Apparently, only variables have to be initialized, where methods never have to be initialized,
Dieter Quickfend wrote:but apparently, variables are initialized to default values even if they are compile-time constants, which I find strange.
Dieter Quickfend wrote:If so, why does Java assign a default value, always? If you initialize your variables explicitly, you're in fact initializing those variables twice, so explicit initialization to null/0 values - instead of constructor initialization - would actually be bad practice instead of good practice, because it's unnecessary and inefficient.
Henry Wong wrote:
Dieter Quickfend wrote:but apparently, variables are initialized to default values even if they are compile-time constants, which I find strange.
No... there is no such initialization to default values if they are compile time constants. In fact, with the latest compilers, there shouldn't even be space allocated for them -- they are constants, not variables, so why not hard wire them in code? They should be treated no different than literals.
Henry
Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Dieter Quickfend wrote:
I changed the static variable to final static, which makes it a compile time constant, no?
Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Dieter Quickfend wrote:
But I still don't get why the method call returns a value. Is it normal that static variables are already initialized implicitly at the time that static variables are initialized explicitly. Does that always happen, or only if the initialization is done using a method call or call to new, ...and is it the same for instance variables?
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |