Java Reference
In-Depth Information
Section 8.10 Garbage Collection
• The Java Virtual Machine (JVM) performs automatic garbage collection (p. 338) to reclaim the
memory occupied by objects that are no longer in use. When there are no more references to an
object, the object is eligible for garbage collection. The memory for such an object can be re-
claimed when the JVM executes its garbage collector.
Section 8.11
static
Class Members
•A
static
variable (p. 338) represents classwide information that's shared among the class's objects.
•
static
variables have class scope. A class's
public
static
members can be accessed through a ref-
erence to any object of the class, or they can be accessed by qualifying the member name with
the class name and a dot (
.
). Client code can access a class's
private
static
class members only
through methods of the class.
•
static
class members exist as soon as the class is loaded into memory.
•A method declared
static
cannot access a class's instance variables and instance methods, be-
cause a
static
method can be called even when no objects of the class have been instantiated.
•The
this
reference cannot be used in a
static
method.
Section 8.12
static
Import
•A
static
import declaration (p. 342) enables you to refer to imported
static
members without
the class name and a dot (
.
). A single
static
import declaration imports one
static
member,
and a
static
import on demand imports all
static
members of a class.
Section 8.13
final
Instance Variables
• In the context of an app's code, the principle of least privilege (p. 343) states that code should be
granted only the amount of privilege and access that it needs to accomplish its designated task.
•Keyword
final
specifies that a variable is not modifiable. Such variables must be initialized when
they're declared or by each of a class's constructors.
Section 8.14 Package Access
• If no access modifier is specified for a method or variable when it's declared in a class, the method
or variable is considered to have package access (p. 344).
Section 8.15 Using
BigDecimal
for Precise Monetary Calculations
• Any application that requires precise floating-point calculations without rounding errors—such as
those in financial applications—should instead use class
BigDecimal
(package
java.math
; p. 346).
•
BigDecimal
static
method
valueOf
(p. 347) with a
double
argument returns a
BigDecimal
that
represents the exact value specified.
•
BigDecimal
method
add
(p. 347) adds its argument
BigDecimal
to the
BigDecimal
on which the
method is called and returns the result.
•
BigDecimal
provides the constants
ONE
(
1
),
ZERO
(
0
) and
TEN
(
10
).
•
BigDecimal
method
pow
(p. 347) raises its first argument to the power specified in its second ar-
gument.
•
BigDecimal
method
multiply
(p. 347) multiplies its argument
BigDecimal
with the
BigDecimal
on which the method is called and returns the result.
• Class
NumberFormat
(package
java.text;
p. 347) provides capabilities for formatting numeric
values as locale-specific
String
s. The class's
static
method
getCurrencyInstance
returns a pre-
configured
NumberFormat
for local-specific currency values.
NumberFormat
method
format
per-
forms the formatting.