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 (Retired) The Model-View-Controller Pattern Adding a Custom Constructor

Ben Morse
Ben Morse
6,068 Points

set mType?

what am I missing? I thought I set it???

Spaceship.java
public class Spaceship {
    public String mType;

    public String getType() {
      return mType;
    }

    public void setType(String type) {
      mType = type;
    }

  public String shuttle() {
    mType = "SHUTTLE";
    return mType;
  }

}

2 Answers

Hadas Dahan
Hadas Dahan
1,213 Points

Constructor should use the name of the class and have no return type

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Ben

Hadas is correct, I thought I would elaborate just a little.

The code challenge is asking you to firstly create a constructor, a constructor is a method that should explicitily use the name of the class as the method name so the class is called Spaceship we must create a method that is public and named Spaceship.

The constructor method is used when instantiating a class (creating a new object from the class) and is usually where any class dependent variables are passed to the object.

I have included the code below to get you through the challenge if you are still stuck

public class Spaceship {
    public String mType;

  public Spaceship(){

   mType = "SHUTTLE";

  }

  public Spaceship(String shuttle){

   mType = shuttle;

  }

    public String getType() {
      return mType;
    }

    public void setType(String type) {
      mType = type;
    }
}

Hope this helps Daniel

Ben Morse
Ben Morse
6,068 Points

Thanks Daniel. I'm still a bit confused...well more like i have a question of "why"?

Why does the constructor have to use the same name as the class? What is the connection? Does it help the compiler find it faster or something?

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Ben

The constructor is a special method, All objects have a default constructor which is used when you use the keyword new in a variable declaration. You can however create your own constructor, usually to pass in new variables or run some set-up type code You will perhaps notice that the constructor method doesn't return anything either this is because it is referring to itself, in short its the naming convention you must follow in order to make the constructor work if you name it anything else you will get a complier error as it will be expecting a return type.

Hope this helps Daniel