Constants and Variables in Java

CONSTANT & VARIABLES

A constant in Java is an unchangeable value give to a variable.


For example:

int AGE = 28;

public class ConstantExample {
   
            private static final int AGE = 28;
            public static void main(String args[])
            {
                       
            System.out.println("Age ="+AGE);          
            }
}

The final keyword:
A final keyword tells Java compiler that the change in value assign to the variable is not allowed.
Constant naming conventions:
Use Upper case letters, for spaces use underscores.
For example:
int AGE = 28;
public keyword with constant:
It says that other classes in same Java project able to access the constant.
private keyword with constant:
It says that other classes in same Java project not able to access the constant.
static keyword with constant:
It makes constant accessible inside main().

LEAVE A REPLY










Related topics

Please use contact page in this website if you find anything incorrect or you want to share more information about the topic discussed above.