3 Replies - 320 Views - Last Post: 04 February 2012 - 07:42 PM Rate Topic: -----

Topic Sponsor:

#1 kikib92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 26-September 11

Passing Object in Constructor

Posted 04 February 2012 - 02:31 PM

How do I pass and set an object in a constructor?
I've tried this, but it isn't correct:
public class Instructor extends Person {
	private Course course;
	public Instructor(String name, int UFID, String dob, Course courses) {
		super(name, UFID, dob);
		this.courses = courses;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Passing Object in Constructor

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7523
  • View blog
  • Posts: 28,896
  • Joined: 27-December 08

Re: Passing Object in Constructor

Posted 04 February 2012 - 02:32 PM

Your Course instance field is named course, not courses. So this.course = courses;.
Was This Post Helpful? 1
  • +
  • -

#3 kikib92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 26-September 11

Re: Passing Object in Constructor

Posted 04 February 2012 - 03:15 PM

What about passing an array of Course objects to a constructor? How would you do that?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7523
  • View blog
  • Posts: 28,896
  • Joined: 27-December 08

Re: Passing Object in Constructor

Posted 04 February 2012 - 07:42 PM

You would define the constructor to accept a Course[] as a parameter. Just as with any method, you can define the constructor to accept any number of parameters of any type. When you invoke the method though, you must pass values of matching type in the order specified.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1