Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSean Walker
1,792 PointsshipType!!!!! last question...adding setter method!
hey everyone! the console keeps giving me syntax errors and i've no idea what i'm doing wrong! help pls!
public class Spaceship {
public String shipType;
public String getShipType() {
return shipType;
public String setShipType(shipType) {
this.shipType = shipType;
}
}
3 Answers
andren
28,558 PointsThere are a couple of issues:
You have declared the
setShipType
method inside thegetShipType
method. You can't place a method inside of another method like that in Java.The method is missing a closing } bracket
You have defined the return type as
String
, but a setter method does not actually return any value, so the return type should be set asvoid
.You are missing the type of the
shipType
parameter, which should beString
.
If you fix those issues like this:
public class Spaceship {
public String shipType;
public String getShipType() {
return shipType;
}
public void setShipType(String shipType) {
this.shipType = shipType;
}
}
Then your code will work.
Nomzamo Sithole
61,838 PointsHey Sean, try the code below:
public class Spaceship {
public String mType;
public String shipType;
public String getType() {
return mType;
}
public String getShipType(){
return shipType;
}
public void setType(String type) {
mType = type;
}
public void setShipType(String ShipType) {
shipType = ShipType;
}
}
Igor Gunger
8,113 PointsExactly, you need to use a void type instead of String one in your set method. Try to make your member variable private.
Sean Walker
1,792 PointsSean Walker
1,792 Pointsi've also tried
public String setShipType(String shipType) { }