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 Initial variables before a try\catch block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Initial variables before a try\catch block" Watch "Initial variables before a try\catch block" New topic
Author

Initial variables before a try\catch block

leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
Hi all!
I have a question considering a simple try\ catch block.
suppose this is my code:



The code won't compile since the variable number may not be initialized. (it only gets value inside the try \ catch block, and therefore may not get a value at all.)
My question is, must i set number to an initialized value?
or is there another way to overcome it.
Thanks!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575




Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
The problem is that fields have a default value, but local variables don't. When you declare a field, the JVM carves out enough memory space for it, and fills that space with 0s, so numbers are initialised to 0 (or 0.0), booleans to false and reference types to null. But there is no such mechanism for local variables, which are put onto a stack. To avoid the local variable simply copying whatever value was on the stack previously, the compiler insists that a local variable be initialised in every "path" through the code it is used in.

Parameters don't suffer this problem because every calling method must supply an argument with a value for every parameter.

You have "try . . . number = . . . " but if that line throws an Exception, then you might get the value for number after catch simply being whatever had been on the stack previously. That could be anything, even total nonsense. The prohibition against using un-initialised local variables is intended to avoid such errors.
sai jyothi
Greenhorn

Joined: Jun 28, 2009
Posts: 28
A local variable must be initialised .if not a Complietime eror occurs( int number=0) must be intialised


scjp5.0 90%
consistent hardwork always gives fruitful results
leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
Thanks all!
I understand what you say about initializing, and it makes a lot of sense.
however, 0 is an int value.
suppose i make my previous code, a method as:


suppose i initialize number to 0.
and suppose an exception occured, since someone typed a char 'x' for example.
in that case, my increase method will return 2, (0 +2) , which is an int value, but no number was actually inputted.
is there a way to not return anything? so i can be sure that a bad input was scanned?

Thanks!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

you're code says this:

public int increase(Scanner input)

that says "This method will return an int".

so, having the method not return anything breaks what you said you'd do.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

That's what exceptions are for.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

You have two choices: first, you could throw an exception (as has already been suggested.) Second, you could use a loop and try again.


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

In which case catching the exception is bad. You should use the check method instead:
Don't forget that call to input.next(). There is something (unless the stream is closed) but it's not an int; input.next() takes that something away. If you don't then the next call to hasNextInt() will return false again, and again, and again.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ernest Friedman-Hill wrote:Second, you could use a loop and try again.


Sorry! I am not getting you
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

seetharaman venkatasamy wrote:
Sorry! I am not getting you


Rob shows a perfect example in his post.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initial variables before a try\catch block
 
Similar Threads
Local variable initialization-Its magic
Variable might not have been initialized
New SCJP mock exam
try {} catch() {} finally {}
Check if my int contains an int (try/catch)