answersLogoWhite

0

Constants in Java can be defined with the finalkeyword or using enum to create an enumerated type.

1. Using final

The final keyword tells the compiler that the value cannot be changed.

final double PI = 3.14159265;

If defined at the class level then static keyword is also used. "static final" is the typical declaration for "constants" in Java.

public class MyClass {

public static final double PI = 3.14159265;

}

2. Using enum

Java 5.0 introduced enumerated types with the enumkeyword, which can also define constants. In their simplest form, these enums look just like their C, C++, and C# counterparts.

enum Season { WINTER, SPRING, SUMMER, FALL }

However, Java enums are far more powerful than their counterparts in other programming languages since they are more than just constant integers and allow you to add arbitrary methods and fields to an enum type.

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

How do you set the constant textarea in java?

we can set the value of constant text area in Java in swing by creating a object of Jframe


How integer classified in java?

int short byte long


How do you compile a java program when it is in another directory?

using javac command and mentioning fully classified class name. Fully classified here means class name along with the package hierarchy in which the class is stored in .java file


What is a pure substance classified as?

A pure substance is classified by having a definite and constant composition. The substance can be either an element or a compound, but what makes the substance pure, is that it does not vary.


What keyword is used to declare a named constant?

Constant in Java refers to a fixed value that doesn’t change during the execution of a program. The value of constants appears right in a program. It is also known as Literals. We use the constants to create values that assign to variables. Constants can make our program easy to read and understood by others. Java does not directly support the constant. To define a variable as a constant, We use the “Static” and “Final” Keywords before declaring a variable. Hope this helps. Thank you


WAP to show the difference between a variable and static variable?

difference between constant and static variables in java


What are the 'Constants' and 'Variables' in Java?

A variable is a memory address that holds a value. A constant is simply a variable that does not change value.


What is public static final variable in java?

-Public to all - Static to access with Class Name - Final to change( constant and not alowed to change) Just use it along with class name. (As implied above, a 'public static final' variable in Java is what other languages would call a Constant. )


List two methods to import java packages?

Java packages can be imported using the import key word in java. There are two ways of importing java programs. 1. you can import the class along with the fully classified class name. Ex: import java.lang.String; 2. you can import all classes in a package using the star symbol. Ex: import java.lang.*;


What are the different parameter passing methods used by c plus plus?

Pass by value, constant value, reference and constant reference. Pass by value is the default in C++ (pass by reference is the default in Java).


What is the difference between a constant and a variable in java programming language?

A constant in Java is defined using the "final" modifier. For instance: final double Density_Of_Water = 1.000; would set Density_Of_Water to 1.000. This value can not be modified throughout the program because "final" told the compiler that this value would not change. A variable however, can be changed by the user throughout the program.


Who discovered the fossils of 'Java Man' pithecanthropus erectus?

The fossils of Java Man, now classified as Homo erectus, were discovered by Eugène Dubois in 1891-1892 on the Indonesian island of Java. Dubois, a Dutch anatomist and paleontologist, made this significant discovery in the Trinil area during his search for human ancestors in Southeast Asia.