Creating New Objects

When you write a Java program, you define a set of classes. As you learned on Day 2, "Object-Oriented Programming," classes are templates that are used to create objects. These objects, which are also called instances, are self-contained elements of a program that contain related features and data. For the most part, you merely use the class to create instances and then work with those instances. In this section, therefore, you learn how to create a new object from any given class.

Remember strings from yesterday? You learned that using a string literal (a series of characters enclosed in double quotation marks) creates a new instance of the class String with the value of that string.

The String class is unusual in that respect. ...

Get Sams Teach Yourself Java 2 in 21 Days, 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.