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 Initializing variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Initializing variables" Watch "Initializing variables" New topic
Author

Initializing variables

memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Hi,
When I do not initialize the reference variable in the below codes, there are a compile time error occured.
the reference variable that I mention : IntBag b
So Why ??

Dani Atrei
Ranch Hand

Joined: Feb 17, 2004
Posts: 73
The compile time errors just tells you that b may not have been initialised, this is to tell you that if you run the program there's likely going to be some problems because it's going to try calling a method with a null reference pointing nowhere, and hence won't work. There's no point compiling something that's not going to do what's intended.
[ February 05, 2006: Message edited by: Dani Atrei ]

Si altas son las torres, el valor es alto - Alberti
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Hi,
I heard that Java does not automatically initialise local variables, though it will initialise any arrays or objects newly created to be stored in local variables. but the code that I sent deny this.
Why???
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Java does not automatically initialize local variables. I don't believe that there is an exception for arrays and objects.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Originally posted by Henry Wong:
Java does not automatically initialize local variables. I don't believe that there is an exception for arrays and objects.

Henry


You should visit the following link.
http://mindprod.com/jgloss/initialisation.html
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Originally posted by memati bas:


You should visit the following link.
http://mindprod.com/jgloss/initialisation.html


The article did *not* say that arrays (that are local) will get automatically initialized. It mentioned that when you initialize the local array variable, Java will automatically initialize the elements, of that array.

Henry
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Oh, thanks for your reply.
Now , I kept the idea.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by memati bas:
Hi,
I heard that Java does not automatically initialise local variables, though it will initialise any arrays or objects newly created to be stored in local variables. but the code that I sent deny this.
Why???


In this case b is indeed a local variable (therefore not initialized to anything) and there is no "object newly created" hence the error.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Initializing variables
 
Similar Threads
Can someone tell me why?
problem with initializing
Largest/Greatest of n numbers
Grade Averaging and Letter Grade Project
what's wrong with this code?