Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final instance variable initialization behaviour

 
aditya chaudhari
Ranch Hand
Posts: 31
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I am preparing for OCAJP 7 exam . My question is about final variable initialization and use.

What I Know : Instance variable get default initialized to values if not initialised with any value
eg. int get initialized to 0 , String initialized to null.

Then Why final instance variable not get initialised with value 0 in following code


Please help me to understand this, where i am doing mistake.?
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final variable always have no default value and must be internalized :
final static - if final static variable not initialed after static initialization block - compilation error
final instance - if final instance variable not initialized after constructor - compilation error


It is a compile-time error if a blank final (§4.12.4) class variable is not definitely assigned (§16.8) by a static initializer (§8.7) of the class in which it is declared.
A blank final instance variable must be definitely assigned (§16.9) at the end of every constructor (§8.8) of the class in which it is declared; otherwise a compile-time error occurs.
jls 7
 
aditya chaudhari
Ranch Hand
Posts: 31
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You very much Sergej
you cleared my doubtS regarding final keyword.

It'll help me a lot.


Thank You
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergej Smoljanov wrote:final variable always have no default value and must be internalized :
final static - if final static variable not initialed after static initialization block - compilation error
final instance - if final instance variable not initialized after constructor - compilation error

Almost correct!

We have 3 kinds/types of variables. Let's discuss each of them.

  • class variables

  • Class variables (also called static fields, static variables,...) get a default value if you don't initialize them. If you use/access (e.g. for printing) a class variable which is not initialized, your code will still compile.
    If you mark a class variable final, you MUST initialize this variable. You'll have 2 ways to do so: assign a value instantly OR in a static initializer block. If you don't initialize a final class variable, your code will NOT compile.

  • instance variables

  • Instance variables (also called data members, member variables,...) get a default value if you don't initialize them. If you use/access (e.g. for printing) an instance variable which is not initialized, your code will still compile.
    If you mark an instance variable final, you MUST initialize this variable. You'll have 3 ways to do so: assign a value instantly OR in a constructor OR in an initializer block. If you don't initialize a final instance variable, your code will NOT compile.

  • local variables

  • Local variables (also called method variables,...) do NOT get a default value. If you use/access (e.g. for printing) a local variable which is not initialized, your code will NOT compile. But if a local variable is declared and not initialized, your code will compile if you don't use/access (e.g. for printing) this local variable. That's a little tricky! These lines can be summarized into 1 simple rule: you MUST assign a value to a local variable before you use/access it; otherwise your code will NOT compile.
    If you mark a local variable final the same rules apply as to the (regular) local variables (so that's easy ).

    I have made a little code snippet where all the different possibilities are mentioned. So you can start experimenting to get a good understanding of variable initialization behavior. (note: CE stands for Compiler Error)


    Hope it helps!
     
    aditya chaudhari
    Ranch Hand
    Posts: 31
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Roel ,

    Thank You very much.

    Your post above is perfect NOTES on final for my OCAJP 7 Exam.

    I play with your code as well , Your code halps me to understand final variable behaviour better.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    aditya chaudhari wrote:Your post above is perfect NOTES on final for my OCAJP 7 Exam.

    Don't forget my royalties
     
    aditya chaudhari
    Ranch Hand
    Posts: 31
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roel De Nijs wrote:
    Don't forget my royalties


    Yes Roel . All your post and other senior members posts in this forum helps me to prepare well.

    I have marked this post as resolved.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic