Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FINAL instance variable

 
Tarun Pahuja
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)as1)as we knw the value of final instance variable is remain constans thrghout
the program,then why instead of one copy..diff copies of its created for different instance ?
2)why can,t we intialized a final instance variable with variable value..
int a;
final int b=a;{why its a error,thgh b is not reference to a?)
3)in c++ we can intialize a instance variable either in constructor or in method,bt
class a
{int a1=5;//give error,bt it works fine in java..why so?
}
4)when the class is loaded..before or after the variable gets their default value?
thankz in advance
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) For true compile-time constants -- final variables whose contents are completely determined when the class is compiled -- there is only one copy. For those members, there's actually no space allocated in each object. But of course it's possible to have a final variable whose value is different in every object.

2) For the exact code you've shown there, the error is because a's value isn't known at the time that b is defined. In general, you can set the value of a final variable to the value of some other variable, as long as the value has been set.

3) I don't understand what you're asking there, exactly, except to say that Java is not all that similar to C++. They have completely different object models, and the process of object creation is completely different, so it's not surprising that the code you're allowed to write is different!

4) Instance variables are set to their initial values when each object is created, which is of course after the class has been loaded.
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Please try to avoid spellings like thankz.
 
Tarun Pahuja
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
means..final non static variable also created when the class is loaded,just like final static variable
can you please elaborate the 1 ans bit more
 
Tarun Pahuja
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much CAMBELL
i will take care of that.
 
Tarun Pahuja
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"2) For the exact code you've shown there, the error is because a's value isn't known at the time that b is defined. In general, you can set the value of a final variable to the value of some other variable, as long as the value has been set".
you said " a's value isn't known at the time that b is defined"..bt as i know final static variable is not created at the time class is loaded
(correct me,if i am wrong)so why b is not intializing with the default value of a[A AND B willLL CREATE AT SAME TIME ,infact in textual order a will create before b in object]?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic