1. Lesson 2 of 4 Object Oriented Programming in Java
1 of 11
Lesson 2: Methods
Author: Kasun Ranga Wijeweera
Email: krw19870829@gmail.com
Date: 2016 December 13
There are two types of methods: instance methods and class methods.
Let‟s look at instance methods first.
Now modify the files as follows.
Student.java
class Student
{
char name;
int age;
double marksMaths;
double marksEnglish;
double average;
static double avg;
void calAverage()
{
average=(marksMaths+marksEnglish)/2;
}
}
2. Lesson 2 of 4 Object Oriented Programming in Java
2 of 11
Test.java
class Test
{
public static void main(String args[])
{
Student x1=new Student();
Student x2=new Student();
x1.name='K';
x1.age=23;
x1.marksMaths=45;
x1.marksEnglish=78;
x2.name='S';
x2.age=24;
x2.marksMaths=56;
x2.marksEnglish=98;
x1.calAverage();
x2.calAverage();
System.out.println(x1.average);
System.out.println(x2.average);
}
}
Execution of following two lines will create two objects.
Student x1=new Student();
Student x2=new Student();
3. Lesson 2 of 4 Object Oriented Programming in Java
3 of 11
Here x1 and x2 store the memory locations of the created two objects
respectively. The attributes of the objects contain their default values.
Following code segment assign user defined values to the attributes.
x1.name='K';
x1.age=23;
x1.marksMaths=45;
x1.marksEnglish=78;
x2.name='S';
x2.age=24;
x2.marksMaths=56;
x2.marksEnglish=98;
The calAverage method is an instance method. That means it always
needs an instance to invoke.
Following line executes the calAverage method with respect to the
object referred by x1.
x1.calAverage();
name = „u0000‟
age = 0
marksMaths = 0.0
marksEnglish = 0.0
average = 0.0
avg = 0.0
name = „u0000‟
age = 0
marksMaths = 0.0
marksEnglish = 0.0
average = 0.0
avg = 0.0
name = „K‟
age = 23
marksMaths = 45
marksEnglish = 78
average = 0.0
avg = 0.0
name = „S‟
age = 24
marksMaths = 56
marksEnglish = 98
average = 0.0
avg = 0.0
x1 x2
x1 x2
4. Lesson 2 of 4 Object Oriented Programming in Java
4 of 11
Following line executes the calAverage method with respect to the
object referred by x2.
x2.calAverage();
Let‟s look at class methods now. The class methods do not need an
object to invoke.
Modify your code as follows.
Student.java
class Student
{
char name;
int age;
double marksMaths;
double marksEnglish;
double average;
static double avg;
name = „K‟
age = 23
marksMaths = 45
marksEnglish = 78
average = 61.5
avg = 0.0
name = „S‟
age = 24
marksMaths = 56
marksEnglish = 98
average = 77
avg = 0.0
x1
x2
5. Lesson 2 of 4 Object Oriented Programming in Java
5 of 11
void calAverage()
{
average=(marksMaths+marksEnglish)/2;
}
static void calAvg(Student s[])
{
int i;
for(i=0;i<s.length;i++)
{
avg+=s[i].average;
}
avg/=s.length;
}
}
Test.java
class Test
{
public static void main(String args[])
{
Student x1=new Student();
Student x2=new Student();
x1.name='K';
x1.age=23;
x1.marksMaths=45;
x1.marksEnglish=78;
6. Lesson 2 of 4 Object Oriented Programming in Java
6 of 11
x2.name='S';
x2.age=24;
x2.marksMaths=56;
x2.marksEnglish=98;
x1.calAverage();
x2.calAverage();
System.out.println(x1.average);
System.out.println(x2.average);
Student x[]=new Student[2];
x[0]=x1;
x[1]=x2;
Student.calAvg(x);
System.out.println(Student.avg);
}
}
The calAvg method is a class method. We have to use keyword static
to denote that it is a class method. It can be invoked as,
Student.calAvg(x);
name = „K‟
age = 23
marksMaths = 45
marksEnglish = 78
average = 61.5
avg = 69.25
name = „S‟
age = 24
marksMaths = 56
marksEnglish = 98
average = 77
avg = 69.25
x1 x2
7. Lesson 2 of 4 Object Oriented Programming in Java
7 of 11
Each of following two lines also does the same thing as above line.
x1.calAvg(x);
x2.calAvg(x);
An instance method can access all the instance attributes and class
attributes declared in the same class in which the method exists.
A class method can only access class attributes declared in the same
class in which the method exists.
Exercise
Implement getGrade method which returns the grade of a student
using the criteria given in the following table.
Average marks range Grade
average ≥ 70 A
40 ≤ average < 70 B
30 ≤ average < 40 C
average < 30 F
Now let‟s discuss a special kind of method called constructor.
The constructor has two main properties: method name is equal to the
class name, no return type.
A constructor can be used to create objects from the class while
assigning user defined values to the attributes.
Modify the files as follows.
8. Lesson 2 of 4 Object Oriented Programming in Java
8 of 11
Student.java
class Student
{
char name;
int age;
double marksMaths;
double marksEnglish;
double average;
static double avg;
Student(char name, int age, double marksMaths, double
marksEnglish)
{
this.name=name;
this.age=age;
this.marksMaths=marksMaths;
this.marksEnglish=marksEnglish;
}
}
Test.java
class Test
{
public static void main(String args[])
{
Student y1=new Student('K',23,45,78);
Student y2=new Student('S',24,56,98);
}
}
9. Lesson 2 of 4 Object Oriented Programming in Java
9 of 11
This time two objects will be created with user defined values for
attributes as follows.
Keyword this in java: this is a reference variable that refers to the
current object.
Now again modify Test.java file only as follows.
Test.java
class Test
{
public static void main(String args[])
{
Student y1=new Student('K',23,45,78);
Student y2=new Student('S',24,56,98);
Student y=new Student();
}
}
This time you will see an error after compiling as shown in the figure
below.
name = „K‟
age = 23
marksMaths = 45
marksEnglish = 78
average = 0.0
avg = 0.0
name = „S‟
age = 24
marksMaths = 56
marksEnglish = 98
average = 0.0
avg = 0.0
y1 y2
10. Lesson 2 of 4 Object Oriented Programming in Java
10 of 11
Default constructor: A constructor that is automatically generated by
the compiler in the absence of any programmer-defined constructors.
Actually what we were using as Student() is the default constructor. It
is invisible and automatically generated in the absence of any
programmer-defined constructors. It is not generated if the
programmer has defined at least one constructor. That is why we got
an error this time. If we need a constructor which does the same thing
as the default constructor we have to modify the class Test.java as
given below.
Test.java
class Student
{
char name;
int age;
double marksMaths;
double marksEnglish;
double average;
static double avg;
11. Lesson 2 of 4 Object Oriented Programming in Java
11 of 11
Student(char name, int age, double marksMaths, double
marksEnglish)
{
this.name=name;
this.age=age;
this.marksMaths=marksMaths;
this.marksEnglish=marksEnglish;
}
Student()
{
}
}
The constructor with empty parameter list and empty body will assign
default values to the attributes and behaves like default constructor.
Now you are not going to see the previous error.
Note that now there are two constructors with the same name.
However the compiler distinguishes the two constructors using the
different parameter lists. This is called “constructor overloading”.
Method overloading: Having more than one method with the same
name, but different method signatures.
Exercise
Implement a method to display the values of attributes in each object.