• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initial variables before a try\catch block  RSS feed

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



 
Marshal
Posts: 57276
175
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A local variable must be initialised .if not a Complietime eror occurs( int number=0) must be intialised
 
leroy tsruya
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
lowercase baba
Bartender
Posts: 12594
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 21205
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what exceptions are for.
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have two choices: first, you could throw an exception (as has already been suggested.) Second, you could use a loop and try again.
 
Rob Spoor
Sheriff
Posts: 21205
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

seetharaman venkatasamy wrote:
Sorry! I am not getting you



Rob shows a perfect example in his post.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
The WEB SERVICES and JAX-RS Course
https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!