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 variable declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "variable declaration" Watch "variable declaration" New topic
Author

variable declaration

aravind kanna
Greenhorn

Joined: Jan 06, 2005
Posts: 1
Even though java automatically assigns value for varialbles why Error occurs if the variables are printed without initialised?
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

I'm moving this to the Java In General (Beginner) forum.

I think what you're asking is this:

Even though java automatically assigns value for varialbles, why [does an] error occurs if the variables are printed without [first being] initialised?

.......................

Variables are given a default value if they are declared at a class level, but they are not assigned a value if they are declared locally (within a method). So you will receive an error if you try to use (for example, print) a local variable that you did not specifically assign a value to.


Check out this example to see what I'm talking about

[ January 06, 2005: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Java automatically initializes member variables, but not local variables; i.e.,



[Jess in Action][AskingGoodQuestions]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: variable declaration