w3resource

Using Static Final Variables in Java: Constants Class Example

Java Static Members: Exercise-4 with Solution

Static Final Variables
Write a Java program to create a class called Constants with a static final variable 'PI' initialized to 3.14159. Create a method to calculate the area of a circle given its radius, using the 'PI' constant. Demonstrate the method in the main method.

Sample Solution:

Java Code:

Constants.java

// Define the Constants class
public class Constants {
    // Declare a static final variable PI
    public static final double PI = 3.14159;

    // Method to calculate the area of a circle given its radius
    public static double calculateArea(double radius) {
        return PI * radius * radius;
    }

    // Main method to demonstrate the calculateArea method
    public static void main(String[] args) {
        // Define a radius
        double radius = 5.0;

        // Calculate the area of the circle
        double area = calculateArea(radius);

        // Print the radius and area
        System.out.println("Radius: " + radius);
        System.out.println("Area: " + area);
    }
}

Output:

Radius: 5.0
Area: 78.53975

Explanation:

  • Define the Constants class:
    • The Constants class is defined using the class keyword.
  • Declare a static final variable PI:
    • The static final variable PI is declared and initialized to 3.14159.
    • static ensures that the variable is associated with the class rather than any instance of the class.
    • final ensures that the value of PI cannot be changed once it is initialized.
  • Method to calculate the area of a circle given its radius:
    • The calculateArea method takes a double parameter radius.
    • The method uses the formula PI * radius * radius to calculate the area of the circle.
  • Main method to demonstrate the calculateArea method:
    • The main method is defined to test the Constants class.
    • A radius is defined and the calculateArea method is called with this radius.
    • The radius and the calculated area are printed to the console.

Note on Java Static Members:

In the above exercise, the static members work by:

  • Static Final Variable: The static final variable PI is associated with the class Constants, not with any instance. The final keyword ensures that the value of PI cannot be altered once it is set, providing a constant value that can be used throughout the class.
  • Static Method: The calculateArea method is static, meaning it can be called without creating an instance of the Constants class. This is appropriate since the method relies only on the static constant PI and does not require any instance-specific data.
  • Main Method: The main method demonstrates how to use the static method calculateArea by calculating and printing the area of a circle with a given radius. This showcases the convenience of using static methods and variables for utility functions and constants.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java Static Members Previous: Using Static Blocks in Java: Initializer Class Example.
Java Static Members Next: Using Static Blocks in Java: Configuration Class 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://www.w3resource.com/java-exercises/static_members/java-static-members-exercise-4.php