File size: 3,900 Bytes
d789391
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
Objects formed from their own classes behave like any other objects. 
That is, objects can be stored in variables and data structures, and their methods can be called in the same way 
as the methods of objects created from classes that come with Java.


-------------------

Objects as method parameters and return values


Objects created from their own classes behave as method parameters and 
return values in the same way as other objects. 
Methods are passed a reference to a creature, so a method can (as a side effect) modify the creatures it receives as parameters.


For example, the class Student and an example of a method that prints student data:

class Student {
    // ATTRIBUTES
    private String name;
    private String studentId;
    private int studyPoints;

    //CONSTRUCTOR
    public Student(String name, String studentId, int studyPoints) {
        this.name = name;
        this.studentId = studentId;
        this.studyPoints = studyPoints;
    }
 
    public String getName() {
        return name;
    }

    public String getStudentId() {
        return studentId;
    }

    public int getStudyPoints() {
        return this.studyPoints;
    }
        
    public void setStudyPoints(int studyPoints) {
        if (studyPoints >= 0) {
            this.studyPoints = studyPoints;
        }
    }
    
    public void addPoints(int completionPoints) {
        if (completionPoints >= 0) {
            this.studyPoints += completionPoints;
        }
    }
}



public class TestClass {
    public static void main(String[] args) {
        Student sally = new Student("Sally Student", "12345", 154);
        printStudent(sally);
    }
    
    public static void printStudent(Student student) {
        System.out.println("Name: " + student.getName());
        System.out.println("St.id: " + student.getStudentId());
        System.out.println("Study points: " + student.getStudyPoints());
    }
}

TestClass outputs:
Name: Sally Student
St.id: 12345
Study points: 154
 




Next, let's define the class Course:
class Course {
    private String identifier;
    private int studyPoints;
     
    public Course(String identifier, int studyPoints) {
        this.identifier = identifier;
        this.studyPoints = studyPoints;
    }

    public String getIdentifier() {
        return identifier;
    }

    public int getStudyPoints() {
        return studyPoints;
    }   
}
...and then a method that takes the course and the student as parameters.

The method adds the credits of the course to the credits of the student:
public class TestClass {
    public static void main(String[] args) {
        Student samuel = new Student("Samuel Student", "12345", 154);
        Course oop = new Course("OOP", 5);
        
        addResult(samuel, oop);
        System.out.println(samuel.getStudyPoints());
    }

    // NEW METHOD HERE
    public static void addResult(Student student, Course course) {
        student.addPoints(course.getStudyPoints());
    }
}

Program outputs:
159






 

Of course, the method can also return a new object. 
The following example creates a new student object by copying the name and student number from the object given as a parameter

public class TestClass {
    public static void main(String[] args) {
        Student samantha = new Student("Samantha Student", "12345", 154);
        Student samantha2 = copy(samantha);
        
        System.out.println(samantha2.getName());
        System.out.println(samantha2.getStudentId());
        System.out.println(samantha2.getStudyPoints());
    }

    // FROM INPUT 'student' object
    // create a 'copy/new student' object
    // retrieving the input's information
    public static Student copy(Student student) {
        Student copy = new Student(student.getName(), 
            student.getStudentId(), 0);
        return copy;
    }
}

Program outputs:
Samantha Student
12345
0