Class

Class

class MyClass {
    // field, constructor, and 
    // method declarations
}

A class that is derived from another class.

Class declarations can include these components, in order: 1. Modifiers such as public, private, and a number of others that you will encounter later. 2. The class name, with the initial letter capitalized by convention. 3. The name of the class's parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent. 4. A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface. 5. The class body, surrounded by braces, {}.

Instantiating a Class

Point originOne = new Point(23, 94);
  • The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.
  • The new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate.
  • The new operator returns a reference to the object it created.

Controlling Access to Members of a Class

Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control: at the top level—public, or package-private (no explicit modifier), at the member level—public, private, protected, or package-private (no explicit modifier).

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package.

Declaring Member Variables

  • Field declarations are composed of three components, in order: 1. Zero or more modifiers, such as public or private. 2. The field's type. 3. The field's name.
  • There are several kinds of variables: member variables in a class (fields), variables in a method or block of code(local variables), variables in method declarations (parameters).
  • All variables must have a type. You can use primitive types such as int, float, boolean, etc. Or you can use reference types, such as strings, arrays, or objects.

Defining Methods

public double calculateAnswer(double wingSpan, int numberOfEngines,
                              double length, double grossTons) {
    //do the calculation here
}
  • The only required elements of a method declaration are the method's return type, name, a pair of parentheses, (), and a body between braces, {}.
  • More generally, method declarations have six components, in order: 1. Modifiers—such as public, private, and others you will learn about later. 2. The return type—the data type of the value returned by the method, or void if the method does not return a value. 3. The method name—the rules for field names apply to method names as well, but the convention is a little different. 4. The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses. 5. An exception list—to be discussed later. 6. The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.
  • Although a method name can be any legal identifier, code conventions restrict method names. By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc. In multi-word names, the first letter of each of the second and following words should be capitalized.

Overloading Methods

public class DataArtist {
    ...
    public void draw(String s) {
        ...
    }
    public void draw(int i) {
        ...
    }
    public void draw(double f) {
        ...
    }
    public void draw(int i, double f) {
        ...
    }
}

The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists.

Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample, draw(String s) and draw(int i) are distinct and unique methods because they require different argument types.

Returning a Value from a Method

public Bicycle seeWhosFastest(Bicycle myBike, Bicycle yourBike,
                              Environment env) {
    Bicycle fastest;
    // code to calculate which bike is 
    // faster, given each bike's gear 
    // and cadence and given the 
    // environment (terrain and wind)
    return fastest;
}
  • A method returns to the code that invoked it when it completes all the statements in the method, reaches a return statement, or throws an exception, whichever occurs first.
  • You declare a method's return type in its method declaration. Within the body of the method, you use the returnstatement to return the value.
  • Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so. In such a case, a return statement can be used to branch out of a control flow block and exit the method.
  • The data type of the return value must match the method's declared return type.

Using the this Keyword

public class Rectangle {
    private int x, y;
    private int width, height;
        
    public Rectangle() {
        this(0, 0, 1, 1);
    }
    public Rectangle(int width, int height) {
        this(0, 0, width, height);
    }
    public Rectangle(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    ...
}

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

Class — Structure map

Clickable & Draggable!

Class — Related pages: