7.1. Instance Variables

In the simplest case, a class is like a structure or a record. An object (an instance of a class) is normally created by new before a call to a class constructor. The constructor looks like a method with the same name as the class being created. For example:

Point p1 = new Point(2, 4);
Color red = new Color(255, 0, 0);

Sometimes, however, the call to new can be hidden. For instance, method calls often return an object; the methods might call new internally or might return an object that already exists. For instance:

OutputStream out = someSocket.getOutputStream();
Point p1 = someWindow.location();

In a very few cases, such as for strings and arrays, Java has shorthand syntax for creating an object. For instance, the ...

Get Core Web Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.