wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Final Attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Final Attributes" Watch "Final Attributes" New topic
Author

Final Attributes

Dimuthu Lakmal
Greenhorn

Joined: Nov 03, 2012
Posts: 8

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4743
    
    7

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Dimuthu Lakmal
Greenhorn

Joined: Nov 03, 2012
Posts: 8

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

Joined: Nov 03, 2012
Posts: 8

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

Joined: Feb 26, 2001
Posts: 4743
    
    7

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Final Attributes