Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get and Set methods for array of objects

 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you return an entire array from a "get" method? For instance, I'm trying to return an array of Instructor objects. I keep getting this error:

Course.java:77: illegal start of expression
this.Instructor[] = Instructor;

This is my code:


Also, how do you use a set method for an array? I'm getting an error in my Student class that says:
./Course.java:77: cannot find symbol
symbol : variable Instructor
location: class Course
this.Instructor[] = Instructor;
^
./Course.java:77: cannot find symbol
symbol : variable Instructor
location: class Course
this.Instructor[] = Instructor;
^

Thanks!
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing special that you got to do with the arrays, they are just another object and you use the setters the way you use for other objects, for example:


So in your Course class you have declared instructor of type Instructor and not an array. But your getters/setters are trying to deal with the Instructor array.

If at all you want to dynamically add the Instructor into a collection- you might want to consider some implementation of List.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic