• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

general doubt

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
consider the following snippet of code



in the above we get compiler error because since i gets default value of 0 and they we are trying to assign it a new value which obviously is wrong because i is declared as final. but if i modify the code as below:



here also if we apply the same analogy, we can say that since a is an array object, it will be initialized to null(refer to null) and within the instance initializer we are referring it to a new object which should give error since a is declared as final and cannot refer to anything else after it has been initialized to its default value which is null. but the code compiles and does not happen the way i assumed. please explain ?
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:consider the following snippet of code



in the above we get compiler error because since i gets default value of 0 and they we are trying to assign it a new value which obviously is wrong because i is declared as final.



So.... based on your explanation, if the variable wasn't declared as final, then it would compile? Try it.... and tell us if it does.


gurpeet singh wrote:
here also if we apply the same analogy, we can say ...


which, as you will see, the analogy is a little flawed ... so this part of the topic is kinda moot.


Henry
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
gurpeet singh wrote:consider the following snippet of code



in the above we get compiler error because since i gets default value of 0 and they we are trying to assign it a new value which obviously is wrong because i is declared as final.



So.... based on your explanation, if the variable wasn't declared as final, then it would compile? Try it.... and tell us if it does.

Henry




it does not compile even if i omit the word final. i remember posting this question earlier and i have the answer that according to JLS you can put certain things in the class declaration. will go through that post. also if i have something like this :




even this compiles. actually what i'am thinking is that i have read that when object initialization occurs , first the memory is allocated , then the variables are set to their default initial values. after that the constructor of the class is run and uptil Object class. here is the link http://www.javaworld.com/javaworld/jw-03-1998/jw-03-initialization.html?page=4. i have also read that instance initializer are executed in textual order. so once the superclass constructor completes , it should execute instance initializer of the Guru class and in that process it will first assign k to 0(default value) and then it will execute constructor body where error will occur. this is how i think . please tell me where i'm wrong or whether the link from which i read object initialization has got something wrong ?
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:
i remember posting this question earlier and i have the answer that according to JLS you can put certain things in the class declaration. will go through that post. also if i have something like this :




even this compiles. actually what i'am thinking is that i have read that when object initialization occurs , first the memory is allocated , then the variables are set to their default initial values. after that the constructor of the class is run and uptil Object class. here is the link http://www.javaworld.com/javaworld/jw-03-1998/jw-03-initialization.html?page=4. i have also read that instance initializer are executed in textual order. so once the superclass constructor completes , it should execute instance initializer of the Guru class and in that process it will first assign k to 0(default value) and then it will execute constructor body where error will occur. this is how i think . please tell me where i'm wrong or whether the link from which i read object initialization has got something wrong ?



Not sure of the point that you are trying to make here..... Final variables are simply variables that don't change. And during initialization, the compiler will just make sure that the generated code only sets the variable exactly once.

While the initialization order (down to the detail that you specified) is interesting, and the fact that the variable has a default value prior to being set is interesting, both facts are also not really related (they apply regardless of whether the variable is declared as final or not).

Henry

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry what i'm trying to say is that , as you said variables have default values so if we consider the code like this :



That means k will have default value of 0, so that means it cannot be set in the constructor. this is what i'm asking . please help ?
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guru ,
  • Final member variable must be initialized at the time of declaration or inside constructor, failure to do so will result in compilation error.
  • Final variables which is not initialized during declaration are called blank final variable and must be initialized on all constructor either explicitly or by calling this(). Failure to do so compiler will complain as "final variable (name) might not be initialized".


  • Read more: http://javarevisited.blogspot.com/2011/12/final-variable-method-class-java.html#ixzz1v6zcAqUq


     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 21518
    84
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    gurpeet singh wrote:Henry what i'm trying to say is that , as you said variables have default values so if we consider the code like this :



    That means k will have default value of 0, so that means it cannot be set in the constructor. this is what i'm asking . please help ?



    The application must set final instance variables exactly once -- And the initial default value doesn't count.

    Henry

     
    gurpeet singh
    Ranch Hand
    Posts: 924
    1
    Fedora Java Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Henry Wong wrote:
    gurpeet singh wrote:Henry what i'm trying to say is that , as you said variables have default values so if we consider the code like this :



    That means k will have default value of 0, so that means it cannot be set in the constructor. this is what i'm asking . please help ?



    The application must set final instance variables exactly once -- And the initial default value doesn't count.

    Henry



    thanks alot Henry. that is where i was confused. i was counting that default value .
     
    gurpeet singh
    Ranch Hand
    Posts: 924
    1
    Fedora Java Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    saloni jhanwar wrote:Hi Guru ,
  • Final member variable must be initialized at the time of declaration or inside constructor, failure to do so will result in compilation error.
  • Final variables which is not initialized during declaration are called blank final variable and must be initialized on all constructor either explicitly or by calling this(). Failure to do so compiler will complain as "final variable (name) might not be initialized".


  • Read more: http://javarevisited.blogspot.com/2011/12/final-variable-method-class-java.html#ixzz1v6zcAqUq




    thanks for the reply and the link Saloni.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic