w3resource

Using Static Methods and Variables in Java: IDGenerator Example

Java Static Members: Exercise-6 with Solution

Static Method with Static Variable
Write a Java program to create a class called "IDGenerator" with a static variable 'nextID' and a static method "generateID()" that returns the next ID and increments 'nextID'. Demonstrate the usage of generateID in the main method.

Sample Solution:

Java Code:

IDGenerator.java

// Define the IDGenerator class
public class IDGenerator {
    // Declare a static variable nextID
    private static int nextID = 1;

    // Define a static method generateID to return the next ID and increment nextID
    public static int generateID() {
        return nextID++;
    }

    // Main method to demonstrate the usage of generateID
    public static void main(String[] args) {
        // Generate and print IDs
        System.out.println("Generated ID: " + IDGenerator.generateID());
        System.out.println("Generated ID: " + IDGenerator.generateID());
        System.out.println("Generated ID: " + IDGenerator.generateID());
    }
}

Output:

Generated ID: 1
Generated ID: 2
Generated ID: 3

Explanation:

  • Define the IDGenerator class:
    • The IDGenerator class is defined using the class keyword.
  • Declare a static variable nextID:
    • The static variable nextID is declared and initialized to 1. This variable will keep track of the next ID to be generated.
  • Define a static method generateID:
    • The static method generateID is defined to return the current value of nextID and then increment nextID.
    • The method uses the ++ operator to increment the value of nextID after returning its current value.
  • Main method to demonstrate the usage of generateID:
    • The main method is defined to test the IDGenerator class.
    • It calls the generateID method multiple times and prints the generated IDs to the console.

Note on Java Static Members:

In the above exercise, the static members work by:

  • Static Variable: The static variable nextID is shared among all instances of the IDGenerator class (if any were created), and it is used to keep track of the next available ID. Being static, it retains its value between method calls and across different usages.
  • Static Method: The static method generateID accesses and modifies the static variable nextID. It can be called without creating an instance of the IDGenerator class, making it easy to generate IDs in a globally accessible manner.
  • Main Method: The main method demonstrates the use of the static method generateID by generating and printing several IDs. This shows how static methods can be utilized to perform operations related to static variables.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java Static Members Previous: Using Static Blocks in Java: Configuration Class Example.
Java Static Members Next: Using Static Blocks for Complex Initialization in Java: Example.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/static_members/java-static-members-exercise-6.php