Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Blank Finals

 
Jui Mahajan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Javaranch exam (roundup) has a question which says " Are instance (member) variables always initialized if they r not explicitly initialzed by the user ?" The ans given is yes. But I think that if an instance variable is a blank final variable, then it will always have to be explicitly initialized by the user, either in the declaration itself or in the constructor ..Pls clarify.
Also, this exam has one more question I am unsure about. The question says
" Can an object of a class access the private variables of another object of the same class ?" The given ans is yes. But I think that this will violate the very essence of Data hiding and encapsulation....so what is the explanation ? Also for that matter, can an object access any members (i.e including public members)of another object ?
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, this exam has one more question I am unsure about. The question says
" Can an object of a class access the private variables of another object of the same class ?" The given ans is yes. But I think that this will violate the very essence of Data hiding and encapsulation....so what is the explanation ? Also for that matter, can an object access any members (i.e including public members)of another object ?

The access modifiers have to do with different classes not objects. Two objects of the same class can access each others members regardless of the access modifier.
 
Dana Hanna
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a final variable isn't initialized at the time of definition, it can't be assigned in a constructor. Therefore, it would be the default for that type, and immutable. I believe that the answer is correct.
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jui Mahajan:
But I think that if an instance variable is a blank final variable, then it will always have to be explicitly initialized by the user, either in the declaration itself or in the constructor ..Pls clarify.

I believe that the right word is definitely assigned. Blank instance final variables are still implicitly initialized but Java requires that they are definitely assigned at the end of the constructor.
You can see from this code that i is initialized to 0 before the start of Test13b constructor.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic