• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

override toString

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I tried to override toString but got the message: unreachable code. Can anyone help m eon this? Thanks

public String toString(){//override toString()

return "Passengers:" + getP();//P,T,M are get method. I changed it to variable--the return variable in the getter method, doesnot help
return "Tank Capacity:" + getT();///wrong message at this line
return "MPG:" + getM();
}


public static void main(String[] args){

Vehicle details= new Vehicle();
System.out.println(details);
}
 
Greenhorn
Posts: 16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A method that declares a return type (in your case a String) can only return one value.
You should concatenate the values and return a single String.
The compiler is warning you that the JVM won’t reach the second return statement

because as soon as the first return is reached the execution would stop for that method and return to the caller.
Have a look at java return value.
http://download.oracle.com/javase/tutorial/java/javaOO/returnvalue.html
 
Ranch Hand
Posts: 34
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rayt,

As Christopher mentioned, you can have only 1 return statement in a method. All code below that would be unreachable because the compiler would not execute anything below return statement.

String objects are immutable. Instead of creating 3 String objects, create 1 StringBuffer object. Append all 3 properties, convert it to String and then return this String.

Thanks,
Badal
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuffer should be avoided; StringBuilder is a better solution. Well, unless you need the synchronization that StringBuffer offers.

If you only concatenate Strings on one single line using an explicit StringBuilder is not necessary though. The compiler will create one for you. is currently compiled into this:
 
We can walk to school together. And we can both read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic