Page 268 - PowerPoint Presentation
P. 268
CAVITE STATE UNIVERSITY
TRECE MARTIRES CITY CAMPUS
Department of Information Technology DCIT 111 - Advanced Programming
public class TestStudent {
public static void main(String[]args) {
Student Student1 = new Student(); // Creating the object
Student Student2 = new Student();
Student1.insertRecord(123, “Chan”; //Initializing object through insertRecord method
Student2.insertRecord(123, “Heaven”);
Student1.displayInformation(); // Calling the method displayInformation
Student2.displayInformation();
}
}
A method is a set of commands which allows you to perform a specific operation in a program.
In other words, a method is a function; something that your class is able to do.
Object and Class Example: Initialization through a Constructor
We will learn about constructors in Java later.
class Employee { What is the purpose of a method parameters?
int ID; - A function can take parameters which are
String Name; just values you supply to the function so that
double Salary; the function can do something to utilize
those values.
void insertInformation (int id, String name, double salary) {
ID = id;
Name = name;
Salary = salary; // Pass by Value and Pass by Reference
} // Pass by value = the method parameters value is copied to another variable.
// Pass by reference = an alias or reference to the actual parameter is pass to the method
// JAVA IS ALWAYS “PASS BY VALUE”
Employee() {
System.out.println(“This is the constructor, I am created whenever object is created!”);
}
void displayInformation() {
System.out.println(“Account ” + ID + “ “ + Name + “ “ + Salary);
} // closing bracket for method displayInformation
} // closing bracket for Class Employee
public class TestEmployee {
public static void main(String[]args) {
Employee Employee1 = new Employee();
Employee Employee2 = new Employee();
Employee Employee3 = new Employee();
Employee1.insertInformation(101, “Christian”, 20000);
Employee2.insertInformation(102, “Poniente”, 25000);
Employee3.insertInformation(103, “Langit”, 30000);
44