File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Local variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Local variables

Kiran Kate
Greenhorn

Joined: Sep 26, 2005
Posts: 2
Why local variables need to be initialized?
why can't Java initialize them to the default value?

Kiran.
nik rb
Greenhorn

Joined: Sep 25, 2005
Posts: 21
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

Joined: Aug 13, 2005
Posts: 11


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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

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.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
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

Joined: Sep 24, 2003
Posts: 1608
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 ]

Tony Morris
Java Q&A (FAQ, Trivia)
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
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

Joined: Oct 25, 2000
Posts: 7292

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