answersLogoWhite

0


Best Answer

There's no such thing as a constant variable (other than as an oxymoron). A named value is either declared constant or it is declared variable, it cannot be both a constant and a variable.

You declare a constant much as you would a variable, except you use the final keyword modifier and name the constant using ALL CAPS and underscores for spaces.

final int HOURS_IN_DAY = 24;

final int DAYS_IN_WEEK;

DAYS_IN_WEEK = 7;

Note that we don't need to assign a constant value at the point of instantiation. However, once assigned we cannot then change the value. It wouldn't be a constant if we could!

The preferred access specifier depends on how useful the constant is. If only one method makes use of the constant, then it should simply be declared within that method as a local constant. If several methods of the same class make use of the constant then declare it a private member of the class. Both methods are used in the following example:

public class MyClass {

private static int HOURS_IN_DAY = 24;

public int hoursInDays (int days) {

return days * HOURS_IN_DAY;

}

public int hoursInWeeks (int weeks) {

final int DAYS_IN_WEEK = 7;

return weeks * DAYS_IN_WEEK * HOURS_IN_DAY;

}

}

Note the use of the static keyword in the constant declaration. It doesn't make sense for every object of a class to store independent copies of the same constant value, thus we must declare class constants as static members so that all objects of the class can share the same instance of the constant and thus share the same value.

If we want the constant to be accessible to our subclasses, then we can declare the constant protected. If we want to make the constant available outside of the class then we can declare it public.

Keep in mind that although constants are not variable, we should only expose what needs to be exposed outside of a class, no more and no less. A constant that provides a class implementation detail has no business being accessible outside of the class in which it is declared.

You should also be aware that the rules governing constants are different for primitive data types and objects. With primitive data types, the value is constant as one would expect. But with objects, only the reference is constant (we cannot refer to a different object) but if the referenced object allows mutations then it is not truly constant. Unfortunately, the only way to create a truly constant object from a mutable class is to subclass the mutable class and render its setters private.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Which access specifier is preferred for constant variables?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the use of public access specifier?

use of public access specifier iswe can access the class members(methods,variables) out side the class using class reference


Which access specifier used for constant variables?

The static modifier, in combination with the finalmodifier, is used to define constants in Java. The finalmodifier indicates that the value of the field cannot change. In addition, the access modifier could be public, protected, or private depending on the scope of the constant that is needed.Example:public static final double PI = 3.141592653589793;


What is an access specifier?

An access specifier is a keyword applied to a variable, or method, which indicates which parts of the program are permitted to access it.


What is an access specifier do?

There is no such thing as an access specifier in Java. There are access modifiers. They specify the access level of the item they modify: public, private, protected.


What is the difference between access specifier and access modifier?

An access modifier is another name for an access specifier, which in object-orientated software is a keyword applied to a variable which indicates which other parts of the programme are permitted to access it.


What is a default access specifier for variable in c sharp?

Default access specifier in c# is private. if you don't specify it automaticaly takes it as private.


Where do you define the access specifier for a java method properties?

We define the access specifier of a function at the place of its method signature(The place we write the method's name).for example,public void sample(){}here "public" is the access specifier of function name-sample.


Which is public to all access class or struct or function or variable?

The default access specifier for a class is private. The default access specifier for a struct is public. It does not matter if it is a function or a variable.


Java Which access modifier is preferred for instance variables?

Usually Private is the preferred access modifier for instance variables. Benefits: 1. No other class can access this variable directly. They can do only through the getter/setter methods 2. Only the methods in that particular class can use this variable


How the jvm is able to access your class even you declare it using default access specifier but the same class is not accessed from other class which is declared in another package?

The JVM knows about all of your classes, no matter what package they are in or what access specifier you declared them with. The access specifier is only used to limit access from other classes.


What is the default access specifier in java?

There is no such thing as an access specifier in Java. There are access modifiers.The default access modifier if unspecified is to allow access to classes in the current package only, except within an interface where the default is 'public'


What is the default access specifier of Java?

There is no such thing as an access specifier in Java. There are access modifiers.The default access modifier if unspecified is to allow access to classes in the current package only, except within an interface where the default is 'public'.