Class In Java

Good day, readers. We will talk about the class in java in today’s topic. Before writing a Java program, it is essential to understand these fundamental ideas. Before these, we also covered decision-making, loop control, and Java methods in case you missed them. Now, hurry up! Other Java-related topics, including Difference between C++ & Java, Operators in Java, Data Types in Java, and many others, have also been briefly discussed in the past. This information will be very helpful to your understanding. So, let’s get to the point without further hesitation:

What is Class in Java?


A class is a collection or grouping of objects with shared relationships, common behaviors, and identical properties. Here, every class object has fundamental class properties. Alternatively, you could state emphatically that a class is a logical template used to create objects.

The keyword “class” must be used in order to create a class. An uppercase letter should always start a class name, and we also need to make sure the Java file name matches the class name.

In Java, a class serves as a logical building block for objects with similar properties and functionalities. As a result, all objects belonging to a class will have the same methods or attributes.

For instance, a particular fruit is an object of the “Fruit” class in the real world. Every fruit in the world has some traits in common, such as the mango, the presence of seeds, or a greenish hue.

The “Fruit” class in Java serves as the template from which all individual fruits can be created. This class includes all fruit attributes, including taste, color, size, shape, and seed type.

A class is declared in the manner described below that is the syntax of a class in java:

class [Class_name] {
     //class body

In a class declaration, the following components are present:

  • Modifiers
  • group name
  • Superclass (if available, the name of a class’ parent)
  • Developed Interfaces (if any)
  • Depending on whether the class extends from a Superclass and/or implements one or more interfaces, appropriate keywords should be used.
  • class body enclosed by curly brackets

Example of class:

// Example of creating a class in Java by GeektoCode
class GeektoCode
    //here we have to initialize some data members
    int id = 1;
    String name = "GeektoCode";
    public static void main(String args[])
        //After that we have to create an object of GeektoCode class
        GeektoCode obj = new GeektoCode();



In the above program we can understand the use of class i.e. first we create a class named GeektoCode. Within the class, we initialize two data members i.e. id & name, and here also the main function from where the program will be started. And within the main function, we have to create the object of the class ‘GeektoCode’ named obj. And after creating the object of the class we have to call the data member by using the Dot (.) operator i.e. ‘’ or ‘’. This is the procedure of creating a class and calling its members by creating the object of the class. And the output of the program that you have shown above.

Key details are:

  • A class is a collection of objects that have similar traits, behaviors, and attributes.
  • It is a factor that controls both the behavior and content of Java Objects.
  • But it is not a physical thing. It merely acts as a prototype, model, or guide from which things can be created.
  • Class does not occupy memory.
  • A class is made up of a group of variables with different data types and a group of methods.
  • Using techniques like inheritance, overriding, and augmenting, a class system enables the program to define a new class (derived class) in terms of an existing class (superclass).

Leave a Reply

Your email address will not be published. Required fields are marked *