Creating Objects – Building Blocks – 1Z0-829 Study Guide

Creating Objects

Our programs wouldn’t be able to do anything useful if we didn’t have the ability to create new objects. Remember that an object is an instance of a class. In the following sections, we look at constructors, object fields, instance initializers, and the order in which values are initialized.

Calling Constructors

To create an instance of a class, all you have to do is write new before the class name and add parentheses after it. Here’s an example:

Park p = new Park();

First you declare the type that you’ll be creating (Park) and give the variable a name (p). This gives Java a place to store a reference to the object. Then you write new Park() to actually create the object.

Park() looks like a method since it is followed by parentheses. It’s called a constructor, which is a special type of method that creates a new object. Now it’s time to define a con-structor of your own:

public class Chick {

public Chick() {

System.out.println(“in constructor”);

}

}

There are two key points to note about the constructor: the name of the constructor matches the name of the class, and there’s no return type. You may see a method like this on the exam:

public class Chick {

public void Chick() { } // NOT A CONSTRUCTOR

}

When you see a method name beginning with a capital letter and having a return type, pay special attention to it. It is not a constructor since there’s a return type. It’s a regular method that does compile but will not be called when you write new Chick().

The purpose of a constructor is to initialize fields, although you can put any code in there. Another way to initialize fields is to do so directly on the line on which they’re declared. This example shows both approaches:

public class Chicken {

int numEggs = 12; // initialize on line String name;

public Chicken() {

name = “Duke”; // initialize in constructor

}

}

For most classes, you don’t have to code a constructor—­the compiler will supply a “do nothing” default constructor for you. There are some scenarios that do require you to declare a constructor. You learn all about them in Chapter 6.

Reading and Writing Member Fields

It’s possible to read and write instance variables directly from the caller. In this example, a mother swan lays eggs:

public class Swan {

int numberEggs; // instance variable

public static void main(String[] args) {

Swan mother = new Swan();

mother.numberEggs = 1; // set variable

System.out.println(mother.numberEggs); // read variable

}

}

The “caller” in this case is the main() method, which could be in the same class or in another class. This class sets numberEggs to 1 and then reads numberEggs directly to print it out. In Chapter 5, you learn how to use encapsulation to protect the Swan class from hav-ing someone set a negative number of eggs.

You can even read values of already initialized fields on a line initializing a new field:

  1. public class Name {
  • String first = “Theodore”;
  • String last = “Moose”;
  • String full = first + last;
  • }

Lines 2 and 3 both write to fields. Line 4 both reads and writes data. It reads the fields first and last. It then writes the field full.