-
Book Overview & Buying
-
Table Of Contents
OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808
By :
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'll look at constructors, object fields, instance initializers, and the order in which values are initialized.
To create an instance of a class, all you have to do is write new before it. For example:
Random r = new Random();
First you declare the type that you'll be creating (Random) and give the variable a name (r). This gives Java a place to store a reference to the object. Then you write new Random() to actually create the object.
Random() 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 constructor of your own:
public class Chick {
public Chick() {
System.out.println("in constructor");
}
}
There are two...
Change the font size
Change margin width
Change background colour