2

I understand that when chaining constructors this must be done on the first line of the main constructor, can someone explain how you would go about the below code. I want to chain to all constructors so that they can all be private other than the main constructor.

public class Flight { int passengers = 0; int seats = 150; double maxKgPerPassenger; private Flight(int passengers) { this.passengers = passengers; } private Flight(int seats) { this.seats = seats; } private Flight(double maxKgPerPassenger) { this.maxKgPerPassenger = maxKgPerPassenger; } public Flight(int passengers, int seats, double maxKgPerPassenger) { this(passengers); this(seats); this(maxKgPerPassenger); } } 
3
  • 1
    you need your first 3 constructor to call the last one with default values Commented Nov 19, 2017 at 20:33
  • private Flight(int seats){ this(20,seats, 20.0D); } Commented Nov 19, 2017 at 20:40
  • you just pass some values you think are correct into the constructor. Plus the argument you're recieving Commented Nov 19, 2017 at 20:41

3 Answers 3

3

A constructor is up to set as many fields as possible or construct an instance completely.

You pick a constructor which has a greater number of arguments and use it within constructors with a smaller amount. For example,

public Flight(double maxKgPerPassenger) { this(0, 0, maxKgPerPassenger); // default, default, maxKgPerPassenger } private Flight(int passengers, int seats, double maxKgPerPassenger) { this.passengers = passengers; this.seats = seats; this.maxKgPerPassenger = maxKgPerPassenger; } 

I want to chain to all constructors so that they can all be private other than the main constructor.

I suggest the opposite way. Make all constructors you are going to use public, but an all-arguments constructor private if you won't be using that.

The approach you tried to adopt is similar to the builder pattern where each method sets a single field. You definitely need to have a look at it if a number of arguments are going to grow up.

Sign up to request clarification or add additional context in comments.

Comments

2

Why do you want to chain constructors ?
As you chain constructors, you have to specify some default values from the constructor with less argument to the invoked constructor with more arguments.
You do the reverse here :

public Flight(int passengers, int seats, double maxKgPerPassenger){ this(passengers); this(seats); this(maxKgPerPassenger); } 

You indeed want to invoke from the constructor with the most argument all other constructors.
It makes no sense and it is not valid either as a constructor can invoke a single other constructor of the same class.

I want to chain to all constructors so that they can all be private other than the main constructor.

Things work in the reverse order.

The actual code doesn't seem to need constructor chaining.
This one is enough :

public Flight(int passengers, int seats, double maxKgPerPassenger){ this.passengers = passengers; this.seats = seats; this.maxKgPerPassenger = maxKgPerPassenger; } 

Suppose that class clients could create Flight instances with two flavors :

  • by passing all parameters
  • by passing only the seats parameter (remains being valued with default values)

You could so write :

public Flight(int passengers, int seats, double maxKgPerPassenger){ this.passengers = passengers; this.seats = seats; this.maxKgPerPassenger = maxKgPerPassenger; } public Flight(int seats){ super(50, seats, 10); // you pass some default values for passengers and maxKgPerPassenger } 

Comments

0

make the default constructor private to prevent constructor instantiation and instantiate objects using a public static method. This way you can work around the constructor restriction. To add a little confusion I converted your constructors to normal methods using the class name by adding void return type. Also consider if want you really want isn't the Builder pattern.

 public class Flight { private int passengers = 0; private int seats = 150; private double maxKgPerPassenger; private Flight() {} private void Flight(int passengers) { this.passengers = passengers; } private void Flight(int seats) { this.seats = seats; } private void Flight(double maxKgPerPassenger) { this.maxKgPerPassenger = maxKgPerPassenger; } public static Flight create(int passengers, int seats, double maxKgPerPassenger) { Flight flight = new Flight(); flight.Flight(passengers); flight.Flight(seats); flight.Flight(maxKgPerPassenger); return flight; } } 

2 Comments

Thanks for the advise, i think i got it working the way i want from pointers. I'm aware this may not be practical or best practice but wanted to know how it works, see below;
<pre><code> package Airline; public class Flight { int passengers = 0; int seats = 150; double maxKgPerPassenger; public Flight() {} public Flight(int passengers, int seats, double maxKgPerPassenger) { this(seats, maxKgPerPassenger); this.passengers = passengers; } private Flight(int seats, double maxKgPerPassenger) { this(maxKgPerPassenger); this.seats = seats; } private Flight(double maxKgPerPassenger) { this.maxKgPerPassenger = maxKgPerPassenger; } } </pre></code>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.