what is a class in java? |Class and Object in Java

what is class in java

what is a class in java?

Hello friends! In today’s post, we will read in detail about Classes and Objects in Java (What is Class and Object in Java?). You read this completely. You will understand this easily. So let’s start:-

Class and Object in Java

Java is an object-oriented programming language. In Java, the program is designed using classes and objects. The object is a physical entity as well as a logical entity whereas class is only a logical entity.

For example:- In real life, a Car is an object. The car has attributes like:- color and size. And it has methods such as drive and brake.

So let us now know about them in detail. First of all, we will read about the class.

Class in Java

Class is a prototype defined by a user from which objects are created.

In other words, a class is a group of objects that have similar properties. This is a blueprint from which objects are created. It is a logical entity. It can’t be physical.”

java class rules

Generally, the following things are followed while declaring a class in java.

Modifiers – A class can have only public or default access.

Class Keyword – The class keyword is used to create a class.

Class name – Its name should start with a capital letter.

Superclass (parent class) – A class can extend only one parent class. The extends keyword is used for this.

Body – The body of the class is inside the curly braces {}. There are variables and methods inside curly braces.

A class can contain fields, methods, constructors, and blocks.

Java class syntax –

class ClassName {

  // fields

    // methods


A simple example of a class –

Suppose a Student is in a class and the student’s name, roll number, and age are its fields, and info() is a method.

class Student


 String name;

  int rollno;

   int age;

   void info(){

    // some code



Object in Java

An Object is an instance of a class. The object represents the class.

In other words, “An entity that has state and behavior is called an object. For example- car, pen, table, chair, bike, mobile, etc. It can be physical or logical.”

An object has the following characteristics:-

State – It is represented by the attributes of the object. It also presents the properties of the object.

Behavior – It is presented by the methods of the object.

Identity – It gives a unique name to the object and enables an object to interact with other objects.

In the real world, a mobile phone is an object and its properties like color, cost, name, etc., while its behavior is:- calling, chatting, etc. So therefore we can say that object is a real-world entity.

Definition of Object

•An object is a real-world entity.

•An object is a run-time entity.

•Object is an entity that has a state and behavior.

•An object is an instance of a class.

creating an object

As I told you earlier, an object is created from a class. In Java, the new keyword is used to create objects.

There are the following three steps to create an object from a class:-

Declaration – First of all the variable is declared.

Instantiation – The new keyword is used to create the object.

Initialization – Finally, new is followed by the class name and parenthesis. This means- calling the class’s constructor to initialize the object.

Java object syntax –

className variable_name = new className();

Example – 

Creating Class and Object

Below you have been given an example of creating an object and a class.

public class Student{      

String name;

int rollno;

int age;

void info(){

System.out.println("Name: "+name);

System.out.println("Roll Number: "+rollno);

System.out.println("Age: "+age);


public static void main(String[] args) {

Student std = new Student();

// Accessing and property value

std.name = "Sohail";

std.rollno = 33;

std.age = 24;

// Calling method





Name: Sohail

Roll number: 33

Age: 24

In the above example, we have created a class named Student. There are three variables inside this student class – name, roll no, and age. And it has an info() method. Here we have used the main() method to create the objects of the Student class.

In how many ways objects can be created in Java?

Objects in Java can be created in the following ways:-

•new keyword

•newInstance() method

•clone() method


•factory method

Anonymous object

Anonymous means – one who does not have a name. An object which does not have any reference is called an anonymous object.

•They are used for immediate method calling.

•These get destroyed after calling the method.

•They are used in many libraries such as – AWT library.


class Calculation{  

void fact(int n){  

int fact=1;  

for(int i=1;i<=n;i++){  



System.out.println("factorial is "+fact);  


public static void main(String args[]){  

new Calculation().fact(5);  




factorial is 120.

Request:- If this post has been useful for you, then definitely share it with your friends and classmates and if you have any questions related to java, then tell me through the comment below. Thank you.

Previous articleswitch case statement in c programming examples|स्विच स्टेटमेंट क्या है?
Next articleintroduction to python programming|(history of python programming-full details)



Please enter your comment!
Please enter your name here