Class and Object in Java

HOME

 

In this blog, we will discuss about Classes and Objects. Java is an Object Oriented Programming language. Class, Objects, Variables, Methods and Constructors are the building blocks of Object Oriented language.

What is a Class?

Class is a user defined template or prototype that used to create objects and define object data types and methods.

What is an Object?

Object is an instance of class. An entity that has state and behavior

Let us consider Phone as the object. The state of Phone is color like grey, black, type like IPhone, Samsung and behavior is calling, sending message, internet browsing.

How to create a class?

To create a class, keyword class is used.

Here, I am creating a class with name Student with variable as Name

public class Student {
          String Name;
}

How to create an Object ?

To create an object, specify class name like Student, object name like stud and use new keyword. 

Student stud = new Student();

Let us create a class and an object of its class and print the value of variable name.

public class Student {
 
          String Name = "Tom";
          public static void main(String[] args) 

          {
                   Student stud = new Student();
                   System.out.println("Name of Student :"+stud.Name);
          }
}
 
Output
Name of Student :Tom

Multiple Objects of a class

We can create multiple objects of a class. In the below example, we have created 2 objects of class Student.

public class Student {
 
          String Name = "Tom";
                public static void main(String[] args)
          {
                          Student stud1 = new Student();
                          Student stud2 = new Student();
                          System.out.println("Name of Student :"+stud1.Name);
                          System.out.println("Name of Student :"+stud2.Name);
              }                          
          }

Output
Name of Student :Tom
Name of Student :Tom

Multiple Classes

We can create a class and then create object of that class in another class. Like, here, we have created a class called Student.java and another class is Student_Demo.java where we will create the object of class Student. This is the better approach than the previous one.

public class Student {
          String Name = "Tom";         
}
public class Student_Test{
          public static void main(String[] args)
          {
                        Student stud = new Student();
                        System.out.println("Name of Student :"+stud.Name);
            }                          
      }

There are multiple ways to initialize the classes and objects

1) Initialize through reference

Here, 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.

public class Student {
          String Name ;
          int Age;
}

public class Student_Test {
      public static void main(String[] args)
          {
                     Student stud = new Student();
                     stud.Name ="Tom";
                     stud.Age=35;       
                     System.out.println("Name of Student :"+stud.Name);
                     System.out.println("Age of Student :"+stud.Age);
              }                          
          }

Output
Name of Student :Tom
Age of Student :35

2) Initializing through method

We are creating the two objects of Student class and initializing the value to these objects by invoking the InsertData() method. Here, we are displaying the state (data) of the objects by invoking the DisplayData() method.

public class Student {
	String Name;
	int Age;

	void InsertData(String n, int a) {
		Name = n;
		Age = a;
	}

	void DisplayData() {
		System.out.println("Name of Student :" + Name);
		System.out.println("Age of Student :" + Age);
	}
}

public class Student_Test {
	public static void main(String[] args) {
		Student stud = new Student();
		stud.InsertData("Tom", 34);
		stud.DisplayData();
	}
}

Output
Name of Student :Tom
Age of Student :34

We can create multiple objects

public class Student {
          String Name ;
          int Age;

          void InsertData(String n, int a)
          {
                   Name =n;
                   Age = a;
          }

          void DisplayData()
          {
                   System.out.println("Name of Student :"+Name);
                   System.out.println("Age of Student :"+Age);
          }
    }  

public class Student_Test {
          public static void main(String[] args)
          {
                     Student stud1 = new Student();
                     Student stud2 = new Student();
                     stud1.InsertData("Tom",34);
                     stud1.DisplayData();  
                     stud2.InsertData("Terry",36);        
                     stud2.DisplayData();
              }                          
          }

Output
Name of Student :Tom
Age of Student :34
Name of Student :Terry
Age of Student :36

3 thoughts on “Class and Object in Java

  1. Thanks Vikas. I'll create a new blog on Constructor. Please keep watching this space.Constructor is a special method that is used to initialize objects. The constructor is called when an object of a class is created.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s