my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Forward reference problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Forward reference problem" Watch "Forward reference problem" New topic
Author

Forward reference problem

Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4





How is this legal?


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

What do you think? what's wrong in it?


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

I'm having a problem with this. Apparently, only variables have to be initialized, where methods never have to be initialized, but apparently, variables are initialized to default values even if they are compile-time constants, which I find strange. Wouldn't it improve performance if variables were only initialized with default values if they weren't initialized at compile-time? 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
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Dieter Quickfend wrote:I'm having a problem with this. Apparently, only variables have to be initialized, where methods never have to be initialized,



Local variables has to be initialized -- mainly because it is easy to check whether they definitely are initialized during compilation. With static and instance variables, the check is not really possible -- so the compiler will generate code to initialize them as the memory for them is allocated.

There is no such a thing as initializing a method.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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.


It's not as bad as you think. Most processors has block zero instructions to wipe out memory. So, its just a couple of cycles during the memory allocation for the object.

Also remember, when the compiler is generating byte codes, it doesn't have access to the subclasses that will be accessing it -- so it is unable to determine whether this "double assignment" is needed or not. Add to that, that it is complicated, it is just easier to wipe out the memory.

Henry
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

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

I changed the static variable to final static, which makes it a compile time constant, no? It compiled fine - and had 0 as output again, so it does assign a default value before it is initialized. I called the forwardReference() method which returns the uninitialized compile time constant forwardRef to assign its value to forwardRef, it prints out 0, so it is initialized. How does this happen, if a compile time constant is not give a default value? It assigns itself a default value through the forwardReference() method, which assigns it to itself.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Dieter Quickfend wrote:
I changed the static variable to final static, which makes it a compile time constant, no?


No. For a variable to be a compile time constant, it must also be assigned to a constant expression. And constant expressions does not make method calls.

Henry
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

Thanks Henry! So the phrase compile-time constant doesn't work here, and I can't use this as a case in a switch statement, for instance.

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?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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?


Reread my first post to this topic.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Forward reference problem