Java

Classes and Objects in Java | Explained

Object-oriented programming has four primary/fundamental concepts i.e. inheritance, encapsulation, abstraction, and polymorphism, and all these concepts revolve around the classes and objects. A class is a blueprint/template that represents the properties and behavior of the objects while the objects are instances of a class. In java, defining the class wouldn’t take any memory until an object of the class is created.

This write-up explained the concept of objects and classes in Java, and in this regard, it demonstrates the following terminologies:

So, without any delay let’s get started!

What is a Class

A Java class is a structure from which an object can be instantiated and it can have various methods and class attributes. A class determines the data fields and actions of an object.

How to Create a Class in Java

Let’s consider the below-given snippet to understand the basic syntax of a class:

public class ClassName {
// class attributes
// member methods
}

In the above snippet public is an access specifier/modifier which specifies that the class is accessible to any other class and to create a class in Java a keyword class along with a legal class name will be used. And within the body of the class, there can be some class attributes and member functions/methods.

How to Create an Object in Java

In Java, a “new” keyword/operator is used to create an object, the basic syntax of the object creation is shown in the following snippet:

public class ClassName {

  public static void main(String[] args) {      
  ClassName obj = new ClassName();
 }  
}

The above snippet shows that to create an object, first initialize the new operator followed by the class name along with the parenthesis and assign it to the object by specifying the class name followed by the object name.

Example

Let’s consider the following piece of code that creates an object of the class, access the value of the class attribute, and finally prints it.

package classesobjects;

public class ClassesObjects {
  String message = "hello world";
  public static void main(String[] args) {
   ClassesObjects obj = new ClassesObjects();
   System.out.println(obj.message);
 }    
}

The above code snippet provides the following output:

The output verifies that the value of class attribute successfully printed using the object of the “ClassesObjects” class.

How to Create Multiple Objects in Java

In Java, we can create more than one object of the same class, the syntax of the object creation will be the same as we followed in the previous example:

This is how you can create multiple objects of a class.

How to Create and Use Multiple Classes in Java

Java allows us to create more than one class to reduce the repetition of code, and to provide better readability and reusability of the code. For instance, we can specify the class attributes and functions in one class and access them from another class. The below-given example will explain the working of multiple classes in java.

Example

The below-given snippet shows that there are some attributes and methods in the first class and the main method is created in the second class:

package classesobjects;

class MultipleClasses {
 int number = 200;
 public void display(){
  System.out.println("This is an example of MultipleClasses");
 }
}

public class ClassesObjects {
 
  public static void main(String[] args) {
   MultipleClasses obj = new MultipleClasses();
   obj.display();
   System.out.println(obj.number);
 }    
}

The object of the first class(MultipleClasses) is created in the main method of the second class(ClassesObjects) to access the attributes and functions of the first class(MultipleClasses).

The complete code and respective output are provided in the following output:

Output verifies the working of multiple classes i.e. members of the first class(MultipleClasses) accessed from the main method of the second class(ClassesObject).

Conclusion

A Java class is a blueprint that describes the properties and behavior of an object. In java, a class can be created using the class keyword and an object can be created using the new keyword. In Java, multiple classes can be created to perform different functionalities and can be accessed from other classes as well. This write-up presents a detailed understanding of Java Classes and objects, furthermore, it explains how to create single or multiple classes and objects in Java.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.