The concept of Class and Object in Java can be categorized into – 

Class in Java

  • A class is a blueprint or template that defines the properties and behaviors of objects.
  • A class serves as a blueprint for creating instances of objects, which are individual entities that belong to a specific class.
  • To declare a class in Java, the class keyword is followed by the class name.
class Classname
// class members (fields and methods) statements
Within a class, we can define member variables (also known as fields) to represent the state or data associated with objects of that class, and methods to define the behaviors or actions that objects can perform.

Object in Java

  • To create an object for a class, we use the new keyword followed by the class name and parentheses.

Classname Objectname= new Classname();

Here, the new keyword creates a new instance of the Classname, and the reference to the newly created object is stored in the variable Objectname. We can then access the fields/variables and methods of the object using the dot (.) operator.

We can create multiple objects for the same class, Thus, we can have multiple instances that encapsulate different data but share the same behavior defined by the class. Here, each object maintains its own separate state.

  • In real-world scenarios, classes and objects are used to model more complex systems and their interactions.

Link for Access Specifiers/Modifiers in Java

Final keyword in Java

  • In Java, the final keyword is used to apply restrictions on classes, methods, and variables.
  • The final keyword in Java serves various purposes, providing ways to create constants, prevent method overriding, and prohibit class inheritance.
  • The keyword final helps in creating immutable constants, ensuring they can’t be changed or extended and thus finally secure the object.
  • The final keyword can be applied on –
    • Final Variables (Constants)
      • When the final keyword is applied to a variable, it makes the variable’s value unchangeable (immutable) throughout the program once it’s assigned a value.
      • The value must be assigned during declaration or in the constructor for instance variables.
      • For example – 

final int num1 = 10;

    • Final Methods
      • When the final keyword is applied to a method, it prevents the method from being overridden by subclasses.
      • It’s commonly used in scenarios where the behavior of a method in a super/base class should not be changed by subclasses.
      • For example –
class BaseClass
     final void process()
          // source codes
    • Final Classes
      • When the final keyword is applied to a class, it prevents the class from being extended/inherited (subclassed).
      • It’s useful when we want to create a class that should not have any subclasses.
      • For example –
final class ClassName
      // Class implementation codes

Method (Function) Overloading in Java

  • Method overloading is a powerful feature in Java that enables developers to write more flexible and intuitive code by defining multiple methods with the same name but different parameter lists to perform similar tasks with varying inputs.
  • Method overloading refers to the ability to have multiple methods in the same class with the same name but different parameters.
  • Method overloading allows methods to perform similar tasks but with different input arguments.
  • To become a method overloaded – 
    • Methods must have the same name within the class.
    • Methods must have different parameter lists (number or types of parameters), i.e. differences in parameters can include different data types, different numbers of parameters, or different order of parameters.
    • Method overloading can’t be based solely on differences in return types or access modifiers.
    • The appropriate method to be executed is determined at compile time-based on the arguments provided.
  • The benefits of method overloading include –
    • Provide different ways to initialize or handle similar operations.
    • Improve code readability by using intuitive method names for different variations of operations.
    • Avoids creating multiple methods with different names for similar functionalities.
    • Provides flexibility and readability in code by allowing the use of the same method name for different variations of operations.
    • Helps in creating clean and maintainable code by logically grouping related functionalities under a single method name.

Static keyword in Java

  • In Java, the static keyword is used to create static variables, static methods, static blocks, or nested static classes that belong to the class itself, rather than to instances (objects) of the class.
  • Static members(static variables and static methods) belong to the class and are shared among all instances equally.
  • Static members are accessed/called using the class name and do not require an instance/object.

Static Variables

    • It is also known as class variables.
    • This variable is shared among all instances of the class.
    • Static variables are initialized to default values when the class is loaded.
    • This variable is accessed using the class name (ClassName.variableName) rather than an instance of the class.
    • This variable is used to maintain a common state for all instances/objects of a class.
    • This variable is Memory-efficient for storing values that remain constant/same for all instances.
    • It is suggested to avoid excessive use of static variables as they can lead to tight coupling and hinder testability in large applications.
    • Example:-
class Example
    public static int count = 0; 

Static Methods

  • Static methods belong to the class rather than to a specific instance (object).
  • Static methods can be accessed/called using the class name (ClassName.methodName()).
  • A static method of a class cannot directly access other non-static members (instance variables or methods or both) of the same class.
  • This method is used in scenarios where a method has common requirements for all instances of a class.
  • Examples:-
class Example
     public static void Display()
             System.out.println(“This is a static method”);

Static Blocks

  • The concept of Static Block is used for initializing static variables.
  • This block is executed only once when the class is loaded into memory.
  • Example:
class Example
                  // Static initialization block codes
                 // Perform some initialization codes


Categories: Java


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.