4.3.1
Objects
TYPES, VALUES, AND VARIABLES
The sample code:
class Point { int[] metrics; }
interface Move { void move(int deltax, int deltay); }
declares a class type
Point
, an interface type
Move
, and uses an array type
int[]
(an array of
int
) to declare the field
metrics
of the class
Point
.
4.3.1 Objects
An
object
is a
class instance
or an array.
The reference values (often just
references
) are
pointers
to these objects, and a
special null reference, which refers to no object.
A class instance is explicitly created by a class instance creation expression
( 15.8), or by invoking the
newInstance
method of class
Class
( 20.3.8). An
array is explicitly created by an array creation expression ( 15.8).
A new class instance is implicitly created when the string concatenation oper
ator + ( 15.17.1) is used in an expression, resulting in a new object of type
String
( 4.3.3, 20.12). A new array object is implicitly created when an array
initializer expression ( 10.6) is evaluated; this can occur when a class or interface
is initialized ( 12.4), when a new instance of a class is created ( 15.8), or when a
local variable declaration statement is executed ( 14.3).
Many of these cases are illustrated in the following example:
class Point {
int x, y;
Point() { System.out.println("default"); }
Point(int x, int y) { this.x = x; this.y = y; }
//
A
Point
instance is explicitly created at class initialization time:
static Point origin = new Point(0,0);
//
A
String
can be implicitly created by a
+
operator:
public String toString() {
return "(" + x + "," + y + ")";
}
}
class Test {
public static void main(String[] args) {
//
A
Point
is explicitly created using
newInstance:
Point p = null;
try {
p = (Point)Class.forName("Point").newInstance();
} catch (Exception e) {
System.out.println(e);
}
38
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved