Static method query .. need help

C

chivesthecat

I have a query regarding a static method. Is it possible to return
multiple objects from the method?

e.g. method getStudentResults below has got 2 Lists and a boolean. How
do I pass them back to the calling method?

public static List getStudentResults(int studentId)
{
List scores = getScores(studentId); // need to pass back
List subjects = getSubjects(studentId); // need to pass back
boolean isQualified = true; // need to pass back

return scores;
}
 
B

Bart Cremers

I have a query regarding a static method. Is it possible to return
multiple objects from the method?

e.g. method getStudentResults below has got 2 Lists and a boolean. How
do I pass them back to the calling method?

public static List getStudentResults(int studentId)
{
List scores = getScores(studentId); // need to pass back
List subjects = getSubjects(studentId); // need to pass back
boolean isQualified = true; // need to pass back

return scores;
}

The way to achieve this is to wrap them in a value object:

public class StudentResults {
private final List scores;
private final List subjects;
private final boolean qualified;

public StudentResults(List scores, List subjects, boolean
qualified) {
this.scores = scores;
this.subjects = subjects;
this.qualified = qualified;
}

public List getScores() {
return scores;
}

public List getSubjects() {
return subjects;
}

public boolean isQualified() {
return qualified;
}
}

Your method then needs to instantiate and return a new value object:

public static StudentResults getStudentResults(int studentId) {
return new StudentResults(getScores(studentId),
getSubjects(studentId), true);
}


Regards,

Bart
 
J

Jan =?ISO-8859-1?Q?Thom=E4?=

Hi,

the common way would be to pass the lists into the function and only have
them filled by the function like:

public static void getStudentResults( int studentId, List scores, List
subjects ) {
...
}

You could as well return a List[] or a List of Lists, or you can also create
a StudentResult object having subject and score as member and return a list
of StudentResults like:

public static List<StudentResult> getStudentResults( int studentId ) {
....
}

public class StudentResult {
String score;
String subject;
}

As you can see there are some possibilities. Which you take is more or less
a matter of personal taste and of course the design of your application.

Best regards,
Jan
 
E

enoesque73

Hi,

I think the fact that you want to return multiple objects suggests
you're trying to make the method do too much?

Initially, it looks like you could have two separate methods:

getStudentScores
getStudentSubjects

But it looks like you've got all Scores in one store, and all Subjects
in another store.

Should your design not associate a Score with a Subject? Have a
SubjectScore class which has private member variables of Subject and
Score.

Just a suggestion that maybe your design could do with a rethink.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,268
Messages
2,571,096
Members
48,773
Latest member
Kaybee

Latest Threads

Top