Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.9k points)

I've seen examples like this:

public class MaxSeconds {

   public static final int MAX_SECONDS = 25;

}

and supposed that I could have a Constants class to wrap constants in, declaring them static final. I know practically no Java at all and am wondering if this is the best way to create constants.

1 Answer

0 votes
by (46k points)

That is pleasant, seemingly even the standard.

(public/private) static final TYPE NAME = VALUE;

where TYPE is the model, NAME is the title in all caps with underscores for spaces, and VALUE is the fixed value;

I extremely suggest NOT placing your constants in their classes or interfaces.

As a bottom note: Variables that are published final and are uncertain can still be improved; yet, the variable can never point at a distinctive object.

For example:

public static final Point ORIGIN = new Point(0,0);

public static void main(String[] args){

    ORIGIN.x = 3;

}

That is fair and ORIGIN would later be a point at (3, 0).

Browse Categories

...