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 Attributes

 
Dimuthu Lakmal
Greenhorn
Posts: 8
Android Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should we initialize final instance before end of execution of object initializer?
If we haven't assigned a value to an instance variable, object initializer assign default value of the data type of the instance.
So why we can't leave initialization of a final instance to assign the default value?
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dimuthu Lakmal wrote:Why should we initialize final instance before end of execution of object initializer?
If we haven't assigned a value to an instance variable, object initializer assign default value of the data type of the instance.
So why we can't leave initialization of a final instance to assign the default value?


The short answer is ... it is defined that way in the specification. Any more than that you will need to ask the designers of the language.

Henry
 
Winston Gutkowski
Bartender
Pie
Posts: 10103
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dimuthu Lakmal wrote:So why we can't leave initialization of a final instance to assign the default value?

Oddly enough, in 11 years of writing Java it's never occurred to me not to assign one myself, so I actually supplied the wrong answer initially.

My opinion: I think the designers wanted to make sure that you can't accidentally forget to assign a value.

Winston
 
Junilu Lacar
Bartender
Pie
Posts: 7306
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there really any sense in declaring something as final and just leaving it to have the default value? I'm hard pressed to come up with a good reason to do something like that and maybe the designers of the language were thinking along the same lines.
 
Dimuthu Lakmal
Greenhorn
Posts: 8
Android Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Dimuthu Lakmal wrote:So why we can't leave initialization of a final instance to assign the default value?

Oddly enough, in 11 years of writing Java it's never occurred to me not to assign one myself, so I actually supplied the wrong answer initially.

My opinion: I think the designers wanted to make sure that you can't accidentally forget to assign a value.

Winston

But we think in that way, designers should design the language to give us an error even when we forget to assign a value to instance variable.
The purpose of using final key word to a instance is to ban assigning a value again to final instance after assigning a value once.
 
Dimuthu Lakmal
Greenhorn
Posts: 8
Android Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Is there really any sense in declaring something as final and just leaving it to have the default value? I'm hard pressed to come up with a good reason to do something like that and maybe the designers of the language were thinking along the same lines.

I'm also thinking so...But while i studying Java as a beginner i had to compile this tiny program(Literally it's a part of a program)...

class A{
final int b;
}

please i need a better explanation to that?
 
Junilu Lacar
Bartender
Pie
Posts: 7306
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dimuthu Lakmal wrote:please i need a better explanation to that?


To my earlier point, the class you have defined there is useless. You don't need to overthink this. It's how Java works, deal with it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic