This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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: 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: 19649

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
Don't get me started about those stupid light bulbs.
subject: Error in return statement
Similar Threads
answers to Mock Exam
R&H questions
equals and == with wrappers
Polymorphism, arguments and instanceof questions
Which Code is Better - Can you help me decide?