«Back to Home

Core Java

Topics

Object And Class In Java

In Java, the object and class is a basic technique of an object oriented programming language. In this, we create a program, using objects and classes.
 
Object

Object is like an entity, which has a state and behavior. For example- mobile, bike, pencil, cat, etc. As a car has states (its color, name and brand) and behavior (running), object is an instance of a class and an object can be tangible and intangible.
  • Object represents identity, which uniquely identifies it. Like one car no. plate is different number of other cars so it’s a car’s identity which is unique.

  • Object represents a value of an object like car name, brand and its color.

  • Object represents the functionality of an object like running.
Creating an Object

In Java, the new keyword is used to create new object.
 
For example
 
Code
  1. public class Scooty { // Scooty is a class.  
  2.   
  3.     public static void main(String args[]) {  
  4.         Scooty s = new Scooty(); // create an object.  
  5.   
  6.     }  
  7. }  
86

In Java, there are many other ways to create an object.
  • By new Keyword
  • By newInstance () method
  • By clone () method
  • By factory method
Class

Class is a template or we can say that it is a blueprint, where objects, data members and methods are defined.
 
Class can contain data member, method, constructor, block, class and an interface.
 
Syntax of class

class <class_name>{
data member;
method;
}
 
For example
 
Code
  1. public class Employee { //employee is a class name  
  2.     int id; //data member  
  3.     String name; //data member  
  4.     int age; //data member  
  5.     public static void main(String args[]) {  
  6.         Employee e1 = new Employee(); //creating an object of Employee  
  7.         System.out.println("id of an employee :" + e1.id);  
  8.         System.out.println("name of an employee :" + e1.name);  
  9.         System.out.println("age of an employee :" + e1.age);  
  10.     }  
  11. }  
87
 
Output

88

Instance variable in Java

Instance variable is declared inside the class but outside the method, constructor or block. It has the widest scope because it is globally visible to the whole class. It is created at the time of object creation. When an instance variable is created, it takes space in the heap memory and it is called through an object and default value is 0.
 
Method in Java

Method is like a function, which is used to represent a behavior of an object. It has many benefits as we can reuse the code and optimize the code.
 
For example
 
Code
  1. public class Employee {  
  2.     int rollno;  
  3.     String name;  
  4.     int salary;  
  5.     void record(int r, String n, int s) { //method    
  6.         rollno = r;  
  7.         name = n;  
  8.         salary = s;  
  9.     }  
  10.     void displayDetails() {  
  11.             System.out.println(rollno + " " + name + " " + salary);  
  12.         } //method    
  13.     public static void main(String args[]) {  
  14.         Employee e1 = new Employee();  
  15.         Employee e2 = new Employee();  
  16.         e1.record(1"David"10000);  
  17.         e2.record(2"James"25000);  
  18.         e1.displayDetails();  
  19.         e2.displayDetails();  
  20.     }  
  21. }  
89

Output

90

63

In the figure, mentioned above, an object takes the space in Heap memory area, e1 and e2 are the reference variables that refer to the object allocated in the memory and the reference variable refers to the allocated object in the heap memory area.
 
Summary

Thus, we learnt object and class are the basic techniques of an object oriented programming language and also learnt how to create a program, using object and class in Java.