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 trial

Android Build an Interactive Story App The Model-View-Presenter Pattern Creating a Data Model

Sean Walker
Sean Walker
1,792 Points

shipType!!!!! 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!

Spaceship.java
public class Spaceship {

  public String shipType;

  public String getShipType() {
        return shipType;
       public String setShipType(shipType) {
        this.shipType = shipType;
    }


}
Sean Walker
Sean Walker
1,792 Points

i've also tried

public String setShipType(String shipType) { }

3 Answers

andren
andren
28,558 Points

There are a couple of issues:

  1. You have declared the setShipType method inside the getShipType method. You can't place a method inside of another method like that in Java.

  2. The method is missing a closing } bracket

  3. 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 as void.

  4. You are missing the type of the shipType parameter, which should be String.

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.

Hey 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
Igor Gunger
8,113 Points

Exactly, you need to use a void type instead of String one in your set method. Try to make your member variable private.