This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes FINAL instance variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "FINAL instance variable" Watch "FINAL instance variable" New topic
Author

FINAL instance variable

Tarun Pahuja
Greenhorn

Joined: Oct 29, 2009
Posts: 6
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38436
    
  23
Welcome to JavaRanch

Please try to avoid spellings like thankz.
Tarun Pahuja
Greenhorn

Joined: Oct 29, 2009
Posts: 6
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

Joined: Oct 29, 2009
Posts: 6
thank you very much CAMBELL
i will take care of that.
Tarun Pahuja
Greenhorn

Joined: Oct 29, 2009
Posts: 6
"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]?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FINAL instance variable