public class MyProg
{
public static void main(String[] args)
{
double PI = 3.14158265358979;
double radius = 2;
double area;
PI = 6.1; // accidents can happen
area = PI * radius * radius;
}
}
|
Problem: we can accidentally change the constant PI (by: PI = .... later in the program)
The keyword final will disallow assigning a value to a variable:
public class demo
{
public static void main(String[] args)
{
final double PI = 3.14158265358979;
double radius = 2;
double area;
PI = 6.1; // Compile error
area = PI * radius * radius;
}
}
|
DEMO: demo/02-elem-prog/08-final/demo.java
Naming convention:
Class Name: MyClass ("Camel case" with first upper case letter)
Method name: myMethod ("Camel case" with first lower case letter)
Variable name: myVariable ("Camel case" with first lower case letter)
Constant: PI (All CAPS)
|