This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
    Bookmark Topic Watch Topic
  • New Topic

Local variables

 
Kiran Kate
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Report post to moderator
Why local variables need to be initialized?
why can't Java initialize them to the default value?

Kiran.
 
nik rb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Report post to moderator
hi.. im not too sure about this but.. i think wat u really want to ask is that
if instance varisbles are initialized to default values y not local variables too
rit??
as i see it.. when an object is created.. a default constructor is called if another constructor is not defined.. this default constructor gives default values to instance variables.
well i tell completely y.. but this is something that happens
im not sure though and by the looks of it my explanation is not complete so if anybody can elaborate then it will be great
i guess the next question to be asked is watif in a non default constructor if i initialize only some of the instance variables then what??

public class constructor{
int a,b,c;

constructor(){
a=0;
b=1;
//i dont initalize c then does c get a default value 0??
}

}

the answer to your question might be hidden in the answer to mine...
 
Phil Dixon
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Report post to moderator


This is probably way off the mark but.... if you don't initialise x (give it a value) you can't add 3 to ie nothing + 3.

[ September 26, 2005: Message edited by: Phil Dixon ]
[ September 26, 2005: Message edited by: Phil Dixon ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Report post to moderator
Originally posted by Phil Dixon:


This is probably way off the mark but.... if you don't initialise x (give it a value) you can't add 3 to ie nothing + 3.

[ September 26, 2005: Message edited by: Phil Dixon ]

[ September 26, 2005: Message edited by: Phil Dixon ]


But what if you do this:



I bet this is why the OP is asking the question. This will compile just fine.

In short, the answer is "because the Java gods said so." There may be other justifications for it, but the original concern is very valid: since member variables have default values, it seems just as easy to provide default values for local variables as well. I honestly can't think of a reason why they didn't do this, but I'm sure there is one.

Layne
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
The separation between class and method variables has to do with what work the compiler can/should do in the process of validating code. In short, the compiler is responsible for type-checking variables at the class level; the programmer is responsible for type-checking at the method level.

That's all initialization really does: guarantee type safety by initializing to a type-specific value. That's why you have byte zero, short zero, integer zero, long zero, etc. The assignment of a default value allows the compiler to confirm that type has been safely established.

Within method space this works gets a little trickier. To make a detailed and technical story short and sweet, it's lots easier to assign the programmer the rather simple task of initializing each type to a proper value than make the compiler do it.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Report post to moderator
1. Why local variables need to be initialized?
2. Why can't Java initialize them to the default value?

Taking the second question first, Java could initialize locals but they chose not to.

The reason is performance, period.

Executing local variable initialization on every method call was deemed too expensive. Local variables can be delcared inside blocks and each block would have to contain setup code, too. So the binaries would bloat and execution would take longer. There is no logical reason for not initializing local variables, just practical constraints.
[ September 26, 2005: Message edited by: Rick O'Shay ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Report post to moderator
The reason locals are not initialized to 'default' is for type-safety and to attempt to meet 'fail early' objectives.
From another perspective, the reason fields are initialized to default values is because it's a (flawed) attempt to provide the optimal solution to the fact that definite assignment (JLS 16) rules cannot be well defined for fields. This is also related to the existence of 'null' as an implicit language defect.
[ September 26, 2005: Message edited by: Tony Morris ]
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Report post to moderator
Tony, an interesting opinion supported nowhere in the literature. If you have ever implemented a high level language in assembly you would have noted that it's the stack resident nature of locals in the stack frame that drives the lack of an initialized local variable strategy.

(Remarks I have interpreted as personal are deleted -- mfe)

[ September 27, 2005: Message edited by: Rick O'Shay ]
[ October 02, 2005: Message edited by: Michael Ernest ]
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
Well I'm sorry to let Rick's comments stand this long, but I've deleted the bitchy ones.

Rick: time for you to shut up, and time for me to tell you so publicly. Your privileges for commenting here ends squarely at the privileges we give everyone to express their own comments -- without being judged by you.

Disagree with each other on facts and design issues all you want. The next time I see either one of you taking a personal shot at the other, though, I'm closing an account.

Enough is enough.
[ October 02, 2005: Message edited by: Michael Ernest ]
 
    Bookmark Topic Watch Topic
  • New Topic