This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java in General and the fly likes default initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "default initialization" Watch "default initialization" New topic
Author

default initialization

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

if i declare a variable but do not initialize it, java supplies a defaut. for reference variables it is assigned null.
ok, so why does the compiler complain that variable url might not be initialized?
i have to change it to
URL url = null;
is it because it is a local variable not a member variable?


SCJP
Visit my download page
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4990
    
    8

That's something the language designers decided: http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5


luck, db
There are no new questions, but there may be new answers.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

there it is
A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified using the rules for definite assignment (§16).

so, the answer is yes, it is because it is a local variable
 
Have you checked out Aspose?
 
subject: default initialization
 
It's not a secret anymore!