In object-oriented programming techniques, we design a program using objects and classes.

An object in Java is the physical as well as a logical entity, whereas, a class in Java is a logical entity only.


What is an object in Java?

An entity that has a state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, etc. It can be physical or logical (tangible and intangible). An example of an intangible object is the banking system.

An object has three characteristics:

  • State: represents the data (value) of an object.
  • Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
  • Identity: An object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. However, it is used internally by the JVM to identify each object uniquely.

For Example, Pen is an object. Its name is Reynolds; the color is white, known as its state. It is used to write, so writing is its behavior.

An object is an instance of a class. A class is a template or blueprint from which objects are created. So, an object is the instance(result) of a class.

Object Definitions:

  • An object is a real-world entity.
  • An object is a runtime entity.
  • The object is an entity that has a state and behavior.
  • The object is an instance of a class.

What is a class in Java?

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical.

A class in Java can contain:

  • Fields
  • Methods
  • Constructors
  • Blocks
  • Nested class and interface

Syntax to declare a class:

class <class_name>{  
    field;  
    method;  
} 

Instance variable in Java

A variable that is created inside the class but outside the method is known as an instance variable. The instance variable doesn't get memory at compile time. It gets memory at runtime when an object or instance is created. That is why it is known as an instance variable.


Method in Java

In Java, a method is like a function that is used to expose the behavior of an object.

Advantage of Method

  • Code Reusability
  • Code Optimization


Object and Class Example: main within the class

In this example, we have created a Student class that has two data members' IDs and names. We are creating the object of the Student class by new keyword and printing the object's value.

Here, we are creating a main() method inside the class.

File: Student.java

//Java Program to illustrate how to define a class and fields  
//Defining a Student class.  
class Student{  
//defining fields  
int id;//field or data member or instance variable  
String name;  
//creating main method inside the Student class  
public static void main(String args[]){  
//Creating an object or instance  
Student s1=new Student();//creating an object of Student  
//Printing values of the object  
System.out.println(s1.id);//accessing member through reference variable  
System.out.println(s1.name);  
}  
}  

Object and Class Example: main outside the class

In real-time development, we create classes and use them from another class. It is a better approach than the previous one. Let's see a simple example, where we are having main() method in another class.

We can have multiple classes in different Java files or a single Java file. If you define multiple classes in a single Java source file, it is a good idea to save the file name with the class name which has main() method

File: TestStudent1.java

//Java Program to demonstrate having the main method in   
//another class  
//Creating Student class.  
class Student{  
int id;  
String name;  
}  
//Creating another class TestStudent1 which contains the main method  
class TestStudent1{  
public static void main(String args[]){  
Student s1=new Student();  
System.out.println(s1.id);  
System.out.println(s1.name);  
}  
}  

3 Ways to initialize the object

There are 3 ways to initialize objects in Java.

  1. By reference variable
  2. By method
  3. By constructor


1) Object and Class Example: Initialization through reference

Initializing an object means storing data into the object. Let's see a simple example where we are going to initialize the object through a reference variable.

File: TestStudent2.java

class Student{  
int id;  
String name;  
}  
class TestStudent2{  
public static void main(String args[]){  
Student s1=new Student();  
s1.id=101;  
s1.name="Sonoo";  
System.out.println(s1.id+" "+s1.name);//printing members with a white space  
}  
}  


We can also create multiple objects and store information in it through reference variable.

File: TestStudent3.java

class Student{  
int id;  
String name;  
}  
class TestStudent3{  
public static void main(String args[]){  
//Creating objects  
Student s1=new Student();  
Student s2=new Student();  
//Initializing objects  
s1.id=101;  
s1.name="Sonoo";  
s2.id=102;  
s2.name="Amit";  
//Printing data  
System.out.println(s1.id+" "+s1.name);  
System.out.println(s2.id+" "+s2.name);  
}  
}  

2) Object and Class Example: Initialization through the method

In this example, we are creating the two objects of the Student class and initializing the value to these objects by invoking the insertRecord method. Here, we are displaying the state (data) of the objects by invoking the displayInformation() method.

File: TestStudent4.java

class Student{  
 int rollno;  
 String name;  
 void insertRecord(int r, String n){  
  rollno=r;  
  name=n;  
 }  
 void displayInformation(){System.out.println(rollno+" "+name);}  
}  
class TestStudent4{  
 public static void main(String args[]){  
  Student s1=new Student();  
  Student s2=new Student();  
  s1.insertRecord(111,"Karan");  
  s2.insertRecord(222,"Aryan");  
  s1.displayInformation();  
  s2.displayInformation();  
 }  
}

3) Object and Class Example: Initialization through a constructor

A constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory.

(i) Java Default Constructor

A constructor is called a "Default Constructor" when it doesn't have any parameter.

In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation.

//Java Program to create and call a default constructor  
class Bike1{  
//creating a default constructor  
Bike1(){System.out.println("Bike is created");}  
//main method  
public static void main(String args[]){  
//calling a default constructor  
Bike1 b=new Bike1();  
}  
}

(ii) Java Parameterized Constructor

A constructor which has a specific number of parameters is called a parameterized constructor.

The parameterized constructor is used to provide different values to distinct objects. However, you can provide the same values also.

In this example, we have created the constructor of the Student class that has two parameters. We can have any number of parameters in the constructor.

//Java Program to demonstrate the use of the parameterized constructor.  
class Student4{  
    int id;  
    String name;  
    //creating a parameterized constructor  
    Student4(int i,String n){  
    id = i;  
    name = n;  
    }  
    //method to display the values  
    void display(){System.out.println(id+" "+name);}  
   
    public static void main(String args[]){  
    //creating objects and passing values  
    Student4 s1 = new Student4(111,"Karan");  
    Student4 s2 = new Student4(222,"Aryan");  
    //calling method to display the values of object  
    s1.display();  
    s2.display();  
   }  
}