File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 19541

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:
subject: Error in return statement
Similar Threads
R&H questions
equals and == with wrappers
Which Code is Better - Can you help me decide?
Polymorphism, arguments and instanceof questions
answers to Mock Exam