• Post Reply Bookmark Topic Watch Topic
  • New Topic

no return  RSS feed

 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the original question was to identify which lines will not compile.But what I cant understand is why I get error "missing return statement" while there is a return statement
can someonne explain?
and the other question I have is why the compiler doesnt mark line 7 (System.out.println(a.add()); // 7) faulty when there is an error in method add?
thanks

[jc: removed formatting inside the code tag. That does not work]
 
Jan Cumps
Bartender
Posts: 2632
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you put the return statement after System.out in your add()?
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Cumps wrote:What happens if you put the return statement after System.out in your add()?

compiles fine
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get two errors:
The first one is what I expected, the second one is surprising as well. The compiler probably sees code without a following return statement, but it would be unnecessary.
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:The first one is what I expected, the second one is surprising as well. The compiler probably sees code without a following return statement, but it would be unnecessary.

thank you
Why doesnt the compiler find an error in the line 7(line I have marked as 7) when there is an error in the method add ?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kevinn lee wrote:Why doesnt the compiler find an error in the line 7(line I have marked as 7) when there is an error in the method add ?

Because there's no error on line 7.
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:
kevinn lee wrote:Why doesnt the compiler find an error in the line 7(line I have marked as 7) when there is an error in the method add ?

Because there's no error on line 7.

having add() which has not compiled is not an error?
thank you
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not on line 7--in the method itself. Marking lines *using* a method that contains a syntax error would be meaningless noise, and distracting. The error doesn't occur when the method is used, the error occurs in the method itself.
 
Dinah Gans
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to add or multiply? Because in your method add() you are multiplying. It looks like from your comments & method name you want to be adding?
 
Jan Cumps
Bartender
Posts: 2632
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinah Gans wrote:Are you trying to add or multiply? Because in your method add() you are multiplying. It looks like from your comments & method name you want to be adding?
The code is doing neither. It prints a string of text, and returns 0. No calculations at all.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Cumps wrote:The code is doing neither. It prints a string of text, and returns 0. No calculations at all.

I think the point was that the method called "add" prints out a multiplication expr.
 
Dinah Gans
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup David, that was my point
 
Jan Cumps
Bartender
Posts: 2632
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
man i was puzzled even reading that program, with indentations it looks a lot more better:

better version of the same program with indentations:


I got the same errors:


As I guess, the compiler first says ok line //5 is unreachable.. Now It moves forward thinking the method is not over yet and finds no return statement, hence it cries foul again.

Here is a one compile error code:




output:
 
ahamed irshad
Ranch Hand
Posts: 31
C++ Notepad Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot reach the 'System.out.println' because you have already invoked 'return'.
 
ahamed irshad
Ranch Hand
Posts: 31
C++ Notepad Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
unreachable statement:


You have written some code that could never be executed, e.g. you put some code immediately after a throw or return statement.
 
ahamed irshad
Ranch Hand
Posts: 31
C++ Notepad Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this link will be very useful regarding compile time errors:

http://mindprod.com/jgloss/compileerrormessages.html#UNREACHABLE
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ahamed irshad,
The OP does know whats causing the unreachable statement error,
The OP is confused about the missing return statement error:
kevinn lee wrote:But what I cant understand is why I get error "missing return statement" while there is a return statement
 
ahamed irshad
Ranch Hand
Posts: 31
C++ Notepad Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i understand.any idea

 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Hi ahamed irshad,
The OP does know whats causing the unreachable statement error,
The OP is confused about the missing return statement error:
kevinn lee wrote:But what I cant understand is why I get error "missing return statement" while there is a return statement

thanks a lot for your time
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!