File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

default initialization

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

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?

Visit my download page
Darryl Burke

Joined: May 03, 2008
Posts: 5115

That's something the language designers decided:

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

Joined: Oct 21, 2000
Posts: 4351

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
I agree. Here's the link:
subject: default initialization
It's not a secret anymore!