0

Is there a way to create getters and setters for these array of objects? I also have two methods to add these ships to the array of objects. One of them looks like. Is this the write way?

`public class College
{
  private Student[] student;
  private Teacher[] teacher;
  int count;
  public College()
  {
    student = new Student[9];
    teacher = new Teacher[9];
    count = 0;
  }

  public Student[] getStudent()
  {
    return student;
  }



  public void setStudent(Student[] student)
  {
    this.student = student;
  }
1
  • Why use hard-coded arrays with compile-time lengths? Maybe consider dynamic collections such as ArrayList, and then simply return an iterable List collection to the caller. Commented Mar 12, 2020 at 17:37

1 Answer 1

1

Yes you can, you would do it just like any other getter/setter, only difference is the return type and the parameters.

public SubmarineClass[] getSubmarine() {
    return submarine;
}

public void setSubmarine(SubmarineClass[] submarine) {
    this.submarine = submarine;
}

public FighterJetClass[] getFighterJet() {
    return fighterJet;
}

public void setFighterJet(FighterJetClass[] fighterJet) {
    this.fighterJet = fighterJet;
}
Sign up to request clarification or add additional context in comments.

2 Comments

How will they be used in the main method then?
@chimmy102 If you had a ShipStorageObject named 'myObj' for example, you could do myObj.getSubmarine(); and similarly for setting you could do myObj.setFighterJet(arg);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.