download materi konstanta
At Java, we utilize key word (keyword) final to point out konstanta.
example:
lingkaran.java |
public class lingkaran { public static void main(String args[]) { final double PHI = 3.14; double radius = 100; System.out.println(“keliling lingkaran berjari - jari”+radius+”adalah”+(2*PHI*radius)); } } |
Final key word betoken point application goes to variable at do once only, point afterwards at variable it can't at changes eternally.
convention:
Name konstanta is uppercase the lot, e.g.:
final double PHI = 3.14 final double CM_PER_INCHI = 2.54; |
Often at Java, konstanta applies to all method / function at one class. Konstanta this ordinary at konstanta's mention brazes (class constant) We can make konstanta class with static's key word final.
example:
Lingkaran.java |
public class lingkaran { public static final double PHI = 3.14; static void test() { lingkaran t = new lingkaran(); double radius = 100; System.out.println(“keliling lingkaran berjari - jari”+radius+”adalah”+t.calcCircumference(radius)); System.out.println(“keliling lingkaran berjari - jari”+radius+”adalah”+t.calcArea(radius)); } public static void main(String args[]){ test(); } static double calcCircumference(double r){ return (2*PHI*r); } static double calcArea(double r){ return (PHI*r*r); } } |
konstanta's definition at do outside main's method(). Thus, konstanta can at utilizes at other methods at same class.
More again, konstanta at samples at declaration utilizes access modifier well worth public, therefore methods at even other class gets to utilize konstanta by points konstanta's name by calls class name as follows:
<nama kelas>.<nama konstanta>. |
On example upon konstanta PHI was pointed to utilize identifier following:
Lingkaran.PHI |
0 comments:
Post a Comment