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 Adding a Custom Constructor

Andre Dippenaar
Andre Dippenaar
497 Points

Adding a default constructor with no parameters

I'm really struggling with this question.

First, add a default constructor without parameters. Inside the constructor, set ship Type to "SHUTTLE".

Spaceship.java
public class Spaceship {
    public String shipType;

    public String getShipType() {
      return shipType;
    }

    public void setShipType(String shipType) {
      shipType = "Shuttle";
    }
}

1 Answer

Hi Andre,

A constructor is a method that takes the name of the class and is public. It returns an instance of the class but that doesn't need adding to the method signature.

In this case we want it to have no parameters and set a value of shipType to SHUTTLE.

So, create a method called the same as the class and make it public:

public Spaceship(){

}

Inside the method set shipType to SHUTTLE:

public Spaceship(){
  shipType = "SHUTTLE";
}

I hope that makes sense.

Steve.

Andre Dippenaar
Andre Dippenaar
497 Points

I was getting stuck in Getters and Setters and not adding the Constructor, thanks