• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Forward reference problem

 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




How is this legal?
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think? what's wrong in it?
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
reply
    Bookmark Topic Watch Topic
  • New Topic