Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

The final and transient variable  RSS feed

 
Ranch Hand
Posts: 77
2
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mr. Deskmukh,

A blank final instance variable must be definitely assigned at the end of every constructor of the class in which it is declared, or a compile-time error occurs (§8.8,§16.9).

Is this rule to assign in all constructors of a class, lets consider in case for e.g. 3 constructors defined?

Thanks.
 
Bartender
Posts: 1206
82
Angular Framework AngularJS Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you compiled?  Did It compile or run successfully ? If not compiled successfully please post complete error message.
 
Marshal
Posts: 64494
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can read all about it in the Java® Language Specification (=JLS).
 
Bartender
Posts: 20766
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A final instance variable can only be assigned a value at one of 2 times:

1. As an expression on the statement that defines it.
2. In a constructor.

That's the final (and there's no pun there) time you can assign a value. And the semantics of Java are that once a value has been assigned to a final variable, you can never assign to it again. That includes trying to re-assign it in a constructor (or a constructor invoking another constructor via "super" or "self"), and it includes assigning a value in a constructor if a value was already assigned on the definition.

Because of this, you absolutely, positively MUST assign the value to a final property before your constructor terminates - or if you aren't using a constructor, in the variable declaration. Because after that point, it's too late.
 
Noorul Hameed
Ranch Hand
Posts: 77
2
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ganesh Patekar wrote:What happened when you compiled?  Did It compile or run successfully ?



Yes. Only It compiled with all constructors as it initialized with final variable.

I just confirm here. Thanks
 
Ganesh Patekar
Bartender
Posts: 1206
82
Angular Framework AngularJS Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:A final instance variable can only be assigned a value at one of 2 times:
1. As an expression on the statement that defines it.
2. In a constructor.

3. An instance initializer ( Note: not included in OCAJP 8 syllabus )
 
Greenhorn
Posts: 27
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding the assignment to a final instance variable in a constructor, I think that maybe it would be easier to think about this if instead of saying it needs to be assigned in a constructor (as one of the options), instead to say "in the construction of an instance".

The reason I think that is suppose you have two constructors which happen to not "interact":



Yes, this code compiles just fine and using the wording "...In a constructor..." sounds fine here.

However, the following code does not compile:



with the following compile error:

$ javac Test.java
Test.java:11: error: variable i might already have been assigned
i = 5;
^
1 error



The only way this code will compile again is if we do not attempt to assign to the final variable in the single-argument constructor:



So using the wording "in the construction of an instance" seems to me a better option.
 
Campbell Ritchie
Marshal
Posts: 64494
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good catch

In the case using this(); which you showed, the variable i is actually initialised twice in one path of execution; the whole idea of final variables is that they must be initialised once.
 
I am Arthur, King of the Britons. And this is a tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!