Using static to declare constants

suggest change

As the static keyword is used for accessing fields and methods without an instantiated class, it can be used to declare constants for use in other classes. These variables will remain constant across every instantiation of the class. By convention, static variables are always ALL_CAPS and use underscores rather than camel case. ex:

static E STATIC_VARIABLE_NAME

As constants cannot change, static can also be used with the final modifier:

For example, to define the mathematical constant of pi:

public class MathUtilities {
    
    static final double PI = 3.14159265358

}

Which can be used in any class as a constant, for example:

public class MathCalculations {
   
    //Calculates the circumference of a circle
    public double calculateCircumference(double radius) {
        return (2 * radius * MathUtilities.PI);
    }

}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents