0
pls give one programming example to explain static CONSTANT variable.
2 odpowiedzi
+ 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));
}
}
0
thank u