pls give one programming example to explain static CONSTANT variable. | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

pls give one programming example to explain static CONSTANT variable.

12th Jul 2016, 12:49 PM
Anup Shetty
2 Respostas
+ 2
Well say you wanted to make a class that did some mathematical calculations. You may have a few values that you will use in those calculations, values that will never change. A constant such as PI for example. To reference it, you wouldn't want to create an object of the class to access the constant. The static keyword will allow you to access the variable in the class as opposed to an instance of the class. The final keyword prevents the value from being changed when it is accessed. public class Circle { final static double PI = 3.142; public static double circumference(double r){ return 2*PI*r; } } public class tester{ public static void main (String[] args) { System.out.println(Circle.circumference(6)); } }
12th Jul 2016, 1:19 PM
Gershon Fosu
Gershon Fosu - avatar
0
thank u
12th Jul 2016, 1:51 PM
Anup Shetty