Previous | Home | Next |
In java you use the keyword final declare a constant.
Example
public class Java TutorialsonstExample { public static void main(String args[]) { final int NUM = 10; int num1= 20; int num3=30; System.out.println("Multiplication is given by" +(NUM*num1*num3)); } }
The keyword final suggests that you can assign the variable once, after that its value is set once for all. The constant name should be in capital letters or upper case.
It is more common in java to declare constants that are available to multiple methods of the class, such constants are called class constants. You can declare a class constant by declaring them static final.
example
public class Java TutorialsonstExample { public static final NUM=10; public static void main(String args[]) { int num1=20; int num3=30; System.out.println("Multiplication is given by"+(NUM*num1*num3)); }
The class constant is defined outside the main method, that means it can be accessed by all the methods of the same class.
Previous | Home | Next |