Create a Simple Java Program using Static keyword

In object-oriented programming, static variables are a powerful feature that allows you to share data among all instances of a class. This blog post will guide you through a simple Java program that demonstrates the use of a static variable to count the number of instances of a class created. Let’s dive in!

Table of Contents

  1. Introduction to Static Variables
  2. Program Overview
  3. How the Program Works
  4. Code Breakdown
  5. Use Cases of Static Variables
  6. Conclusion

Introduction to Static Variables

In Java, a static variable is associated with the class itself rather than with any particular instance. This means:

  • Shared State: All instances of the class share the same static variable.
  • Class-Level Scope: The static variable can be accessed directly by the class name without creating an instance.

Static variables are useful when you need a shared state or a common property across all instances of a class.


Program Overview

Here’s the program that demonstrates the use of a static variable:

class Client {
    static int count;
    Client() {
        count++;
    }
}

public class Main {
    public static void main(String[] args) {
        Client c1 = new Client();
        Client c2 = new Client();
        Client c3 = new Client();
        Client c4 = new Client();
        Client c5 = new Client();
        Client c6 = new Client();

        System.out.println(Client.count);
    }
}

This program creates multiple instances of the Client class and prints out the total number of instances created.


How the Program Works

The program demonstrates the use of a static variable count to keep track of how many instances of the Client class have been created. Here’s how it works:

  1. Static Variable: static int count; declares a static variable count in the Client class. This variable is shared across all instances of Client.
  2. Constructor: The constructor Client() increments the count variable every time a new instance of Client is created.
  3. Main Method: The main method in the Main class creates six instances of Client. After creating the instances, it prints the value of Client.count.

The output will be 6, indicating that six Client objects were created.


Code Breakdown

Let’s break down the code into smaller parts for a better understanding.

Client Class

class Client {
    static int count;
    Client() {
        count++;
    }
}
  • Static Variable: static int count; defines count as a class-level variable that is shared among all instances.
  • Constructor: Client() is called whenever a new instance is created. It increments the static variable count.

Main Class

public class Main {
    public static void main(String[] args) {
        Client c1 = new Client();
        Client c2 = new Client();
        Client c3 = new Client();
        Client c4 = new Client();
        Client c5 = new Client();
        Client c6 = new Client();

        System.out.println(Client.count);
    }
}
  • Creating Instances: Six instances of Client are created.
  • Printing Count: The value of the static variable count is printed using System.out.println(Client.count);.

Use Cases of Static Variables

Static variables can be useful in various scenarios:

  • Counting Instances: As shown in the example, to keep track of the number of instances created.
  • Shared Data: To share data among all instances of a class, like configuration settings or constants.
  • Utility Classes: In utility or helper classes to maintain common data or provide a shared functionality.

Example: Configuration Settings

class Config {
    static String appName = "MyApp";
    static String version = "1.0.0";
}

Here, appName and version can be accessed without creating an instance of Config.


Conclusion

The use of static variables in Java provides a way to share data and manage common properties across all instances of a class. The simple program discussed in this post demonstrates how a static variable can be used to count the number of instances of a class. This concept is fundamental in Java and is widely used in real-world applications for various purposes like configuration management, counting instances, and more.

By understanding and applying static variables, you can write more efficient and organized Java code, especially in scenarios where shared data or common state is required.


Feel free to experiment with the code and extend its functionality. Static variables can be a valuable tool in your Java programming toolkit!


For more Java tutorials and programming tips, check out our other blog posts and guides.

Leave a Comment

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

Scroll to Top