Instance Methods in Java Programming
OOPs in Java
In this lesson, we will understand what is Instance Method in Java Programming and how to create them along with some examples.
What are Instance Methods in Java?
Instance methods are used to store or process data stored in instance variables and are used only by the object of the class.
Note: When a function is declared in a class, it is called Method.
Example
import java.util.Scanner;
class Record
{
// creating instance variables
private int rollno;
private String name;
private int age;
// Creating a public instance method to store data in private instance variables
public void inputdata()
{
Scanner sc=new Scanner(System.in);
System.out.print("Roll No: ");
rollno=sc.nextInt();
sc.nextLine(); // consume the new line charecter \n that is left by the nextInt() method
System.out.print("Name: ");
name=sc.nextLine();
System.out.print("Age: ");
age=sc.nextInt();
}
// Creating a public instance method to display data of private instance variables
public void displydata()
{
System.out.println("Roll No: " + rollno);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Example
{
public static void main(String args[])
{
Record x =new Record();
x.inputdata();
x.displydata();
}
}
Output
Roll No: 1 Name: Martin Age: 15 Roll No: 1 Name: Martin Age: 15
In the above example, we have declared the instance variables private because we do not want to store values in instance variables from outside the class.
We have declared public methods to store and display data on the output screen. We will use these public methods using the object of the class to store and process data stored in instance variables. In this way, we can hide our instance variables from accessing outside the class.
Let's take another example that can be coded as class and object in Java using instance variable and instance methods.
Suppose we want to store the details of a student like a roll no, name, class, marks obtained in three different subjects (English, Maths, Computer), total and percentage obtained.
For the above question, we will create a class called Student with the following instance variables:
- roll: for storing roll no of the student.
- name: for storing name of the student.
- cls: for storing class of the student.
- eng: for storing marks obtained in english.
- math: for storing marks obtained in math.
- comp: for storing marks obtained in computer.
- total: for storing total marks obtained.
- per: for storing total percentage obtained.
We will also create three instance methods inside the Student class for processing the instance variables, and they are:
- inputdetails(): for storing information in the instance variables.
- calculate() for calculating and storing the total and percentage obtained.
- display(): for displaying the information stored in the instance variables on the screen.
Example
import java.util.Scanner;
class Student
{
// creating instance variables
private int rollno;
private String name;
private int Class;
private int eng;
private int math;
private int comp;
private int total;
private float per;
// Creating a public instance method to store data in private instance variables
public void inputdata()
{
Scanner sc=new Scanner(System.in);
System.out.print("Roll No: ");
rollno=sc.nextInt();
sc.nextLine(); // consume the new line charecter \n that is left by the nextInt() method
System.out.print("Name: ");
name=sc.nextLine();
System.out.print("Class: ");
Class=sc.nextInt();
System.out.print("English: ");
eng=sc.nextInt();
System.out.print("Math: ");
math=sc.nextInt();
System.out.print("Computer: ");
comp=sc.nextInt();
}
// Creating a public instance method to calculate and store total and percentage
public void calculate()
{
total=eng+math+comp;
per=(total/300.0f)*100;
}
// Creating a public instance method to display data of private instance variables
public void displydata()
{
System.out.println("Roll No: " + rollno);
System.out.println("Name: " + name);
System.out.println("Class: " + Class);
System.out.println("English: " + eng);
System.out.println("Math: " + math);
System.out.println("Computer: " + comp);
System.out.println("Total Marks: " + total);
System.out.println("Percentage: " + per);
}
}
public class Example
{
public static void main(String args[])
{
Student x=new Student();
x.inputdata();
x.calculate();
x.displydata();
}
}
Output
Roll No: 1 Name: Martin Class: 5 English: 78 Math: 84 Computer: 91 Roll No: 1 Name: Martin Class: 5 English: 78 Math: 84 Computer: 91 Total Marks: 253 Percentage: 84.33333
Array of Objects
Suppose we want to store the records of 10 students. In this situation, we will create a class and 10 objects of that class to store individual student records in individual objects. But that will be a lengthy process. To make it short, we will create an array of objects so that each array element will become an individual object. We can easily access any object of the object array using its index value. See the example given below.
Example
import java.util.Scanner;
class Student
{
// creating instance variables
private int rollno;
private String name;
private int Class;
private int eng;
private int math;
private int comp;
private int total;
private float per;
// Creating a public instance method to store data in private instance variables
public void inputdata()
{
Scanner sc=new Scanner(System.in);
System.out.print("Roll No: ");
rollno=sc.nextInt();
sc.nextLine(); // consume the new line charecter \n that is left by the nextInt() method
System.out.print("Name: ");
name=sc.nextLine();
System.out.print("Class: ");
Class=sc.nextInt();
System.out.print("English: ");
eng=sc.nextInt();
System.out.print("Math: ");
math=sc.nextInt();
System.out.print("Computer: ");
comp=sc.nextInt();
}
// Creating a public instance method to calculate and store total and percentage
public void calculate()
{
total=eng+math+comp;
per=(total/300.0f)*100;
}
// Creating a public instance method to display data of private instance variables
public void displydata()
{
System.out.println("Roll No: " + rollno);
System.out.println("Name: " + name);
System.out.println("Class: " + Class);
System.out.println("English: " + eng);
System.out.println("Math: " + math);
System.out.println("Computer: " + comp);
System.out.println("Total Marks: " + total);
System.out.println("Percentage: " + per);
}
}
public class Example
{
public static void main(String args[])
{
Student x[]=new Student[10]; // Declared an object array
int i;
// Store information in individual objects
for(i=0; i<10; i++)
{
x[i]=new Student(); // Initializing the object
x[i].inputdata();
x[i].calculate();
System.out.println();
}
// Display information of each object
for(i=0; i<10; i++)
{
x[i].displydata();
System.out.println();
}
}
}