missing return statement compile error

M

Matt

In case1, it has the "missing return statement" compile error. But if
I do case 2 or case 3. Then no more compile errors.

What's the rationale? please advise. thanks!!

//case 1
public String returnTest(String s)
{
//compile error: missing return statement
if (s.equals("test"))
return "test";
}

//case 2
public String returnTest(String s)
{
if (s.equals("test"))
return "test";
else
return "non-test";
}

//case 3
public String returnTest(String s)
{
if (s.equals("test"))
return "test";
return "non-test";
}
 
W

Woebegone

Matt said:
In case1, it has the "missing return statement" compile error. But if
I do case 2 or case 3. Then no more compile errors.

What's the rationale? please advise. thanks!!

//case 1
public String returnTest(String s)
{
//compile error: missing return statement
if (s.equals("test"))
return "test";
}

//case 2
public String returnTest(String s)
{
if (s.equals("test"))
return "test";
else
return "non-test";
}

//case 3
public String returnTest(String s)
{
if (s.equals("test"))
return "test";
return "non-test";
}

In case (1), no return statement exists for the case !s.equals("test"), so
not all potential paths are covered. As written, the other two cases are
equivalent. The rationale would be something along the lines of "a method
declared to return a value of type T must return a value of type T."

HTH,
Sean.
 
R

Roedy Green

//case 1
public String returnTest(String s)
{
//compile error: missing return statement
if (s.equals("test"))
return "test";
}

what value is it supposed to return if s is not equal test? You have
not specified.
 
A

Andrea Spinelli

(e-mail address removed) (Matt) wrote in
public String returnTest(String s)
{
//compile error: missing return statement
if (s.equals("test"))
return "test";
}

AClass a = new AClass();
String x = a.returnTest("foo");
// in your opinion, what the value of x should be?

public String returnTest(String s)
{
if (s.equals("test"))
return "test";
else
return "non-test";
}
String y = a.returnTest("foo");
// now y is "non-test"
public String returnTest(String s)
{
if (s.equals("test"))
return "test";
return "non-test";
}
String y = a.returnTest("foo");
// now y is "non-test"

Do you see the difference now???

If you don't, you are up shit creek
[from _The Blues Brothers_]

Andrea
 
Joined
Mar 19, 2007
Messages
1
Reaction score
0
hello everyone. I m new to java and specially to EJB.was working on a piece of code but i m getting the same missing return statement error.can anyone please help


public String ins_Deposit(String userid,int amount,String branch,String bank) throws SQLException
{
Statement ps = null;
try
{
ps = conn.createStatement();
String query="INSERT INTO BANK_TRANSACT VALUES('"+userid+"',sys_date,"+amount+",'D','"+branch+"','"+bank+"')";
int result = ps.executeUpdate(query);
if(result>0)
return "Transaction successfully completed,amount will be credited to your account within 24hrs. ";
else
return "Bank timings are between 8 AM to 8 PM IST,Record not inserted";
}
catch(SQLException e)
{
System.out.println("Unable to get Data: " + e);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top