wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Error in return statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error in return statement" Watch "Error in return statement" New topic

Error in return statement

Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
I am getting the error as:
InstanceofExam.java:15: variable taxrate might not have been initialized
return taxrate;

the code containing return statement is :

public float Tax(Object e){
float taxrate;
if( e instanceof manager){
if( e instanceof engineer){
return taxrate;

Please suggest any solution.

Jijesh T Das

Joined: Sep 27, 2007
Posts: 11
You need to initialize the variable taxrate. Java compiler checks for this and hence the error.

Modify like float taxrate = 0;
Parambir Singh
Ranch Hand

Joined: Sep 05, 2004
Posts: 40

You always need to set some value for a variable before using it in any statement. If you are not sure what value to assign, you can initialize a variable to null:

However, you can't set the primitive types (int, float, boolean etc.) to null. So you'll have to initialize them to some value as per your program's logic (0 for int, false for boolean etc.)

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

<edit>by the way,</edit>
@OP: Dont use float or double for a precise calculation . read this
Rob Spoor

Joined: Oct 27, 2005
Posts: 19677

As for why, ask yourself this question: what will the value of taxrate be when e is neither a manager nor an engineer? Unlike static and instance fields, local variables do not get default values.

How To Ask Questions How To Answer Questions
I agree. Here's the link: http://aspose.com/file-tools
subject: Error in return statement