Q:

Declaring a constant in Java

0

Syntax:

    final static datatype constant_name = value;

Note: While declaring a constant – it is mandatory to assign the value.

Example:

    final static int MAX = 100;

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Java code to declare and print the constant

// Java code to declare and print the constant
public class Main {
    //integer constant
    final static int MAX = 100;
    //string constant
    final static String DEFAULT = "N/A";
    //float constant
    final static float PI = 3.14f;

    public static void main(String[] args) {
        //printing the constant values
        System.out.println("value of MAX = " + MAX);
        System.out.println("value of DEFAULT = " + DEFAULT);
        System.out.println("value of PI = " + PI);
    }
}

Output

value of MAX = 100
value of DEFAULT = N/A
value of PI = 3.14

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now