Java Reference
In-Depth Information
seen examples of static methods in the
Math
class. In fact,
Math
is a class of
nothing but static member variables and static methods. The
Math
class itself
cannot even be instantiated. The only way to access the various constants and static
methods in the
Math
class is with the
Math.constant
and
Math.method()
syntax.
If a class property is also declared
final
, then it becomes a constant value
that cannot be altered. In the following example, the static data field
PI
is now a
fixed constant. By convention, constants are normally written with all uppercase
letters.
class ConstHolder {
public final static double PI = 3.14;
}
3.6 More about primitive and reference variables
Primitive data type operations deal only with value. For example, the following
code shows that assigning primitive data variable
i
to another primitive named
j
simply passes a copy of the value in
i
into
j
. That is, a datum's value is passed,
not a reference or pointer to a data location.
int i = 1; // Variable i holds the value 1.
int j = i; // Now j holds 1 also, that is, i's value is
// copied to j
i = 2; // Now i is 2, but j still holds the value 1
Similarly, in method parameters, primitive variables are passed by value. That is,
a copy is made and passed to the method. Changes to the passed value inside the
method cannot affect the value in the calling code.
The following snippet creates an instance of
AClass
and then invokes the
change()
method with an
int
parameter.
...
a method in some class
...
int i = 2;
AClass a1 = new AClass ();
a1.change (i);
int m = i; // m = 2, no matter what happens inside
// a1.change()
System.out.println ("i ="+ i);
...
Search WWH ::
Custom Search