Classes
- A class defines a common structure for many objects. The structure is made up of variables, constructors, and methods which, collectively, are called the “members” of a class.
- Defining a class also defines a new type that can be used anywhere a type is needed: in variable declarations, as the return type of a method, or as the type of element on an array or
ArrayList
.
- An object is a single instance of a class whose data lives somewhere in memory distinct from all other objects.
Instance variables
- An object’s state is stored in its instance variables and each object has its own values in its instance variable.
- Instance variables create a “has-a” relationship between the object and the values stored in its instance variables, as in a
Point
has an x and y value. (This is often contrasted to the “is-a” relationship created when one class extends
another which will get too when we talk about inheritance.)
- Instance variables can be marked as either
public
or private
before the variable type, e.g private int n
. Typically they are made private
.
Constructors
- Constructors are invoked to create objects. A constructor’s signature consists of the constructor name, which always matches the class name, and a parameter list.
- Every object is created by calling a constructor using the keyword
new
followed by the class name and an argument list that corresponds to one of the class’s constructors.\note{String objects can also be created by writing a literal string in your program. They are the only class that gets special treatment from the Java compiler. And autoboxing of primitive types can cause new instances of the wrapper types to be created without explicitly calling a constructor.}
- The job of a constructor is to initialize the state of an object and leave it in a usable state. Often constructors assign the values passed as arguments to the constructor to the object’s instance variables.
- Constructors have parameter lists that work just like method parameter lists, defining local variables that are initialized with the values of the arguments passed to a call to the constructor. The arguments should provide the data needed to initialize the object’s state.
- When no constructor is written, Java provides a no-argument constructor and all instance variables are set to the default value for their type. (0 for int, 0.0 for double, false for boolean, and null for all reference types.)
- When a mutable object is passed as an argument to a constructor and stored in an instance variable, any mutations made to that object by code in the class will change the state for all code that has a reference to that object. Sometimes it’s necessary to instead store a copy of the argument to avoid changing the original object out from under other code. Other times that’s the whole point of being passed the object.
Methods
- An object’s behavior—what we can do with the object—is defined by the methods in its class.
- Instance methods are called on objects of the class while
static
methods are called on the class itself.
- Instance methods have access to all of an object’s instance variables.
- Methods that take no arguments and simply return the value of an instance variable are called “getters” or, more formally, “accessors”. They are used to give code outside the class access to the object’s state without making instance variables public.
- Methods that return
void
, take a single argument, and assign it to the value of an instance variable are called “setters”. More generally, methods that change any part of an object’s state are called “mutators”.
static
members
- Variables and methods can be either static, marked with the
static
keyword, or non-static, also known as “instance” variables and methods.
- Static variables and methods can be marked as either
public
or private
and then marked with the static
keyword before the variable type or the return type of a method, e.g. public static int
.
- Each static variable represents a single value stored in the class itself and shared by all instances of the class.
- Static methods can only access static variables and call other static methods.\note{Static methods can, like any code, access instance variables and invoke instance methods if they have a reference to an instance of a class such as by being passed one as an argument or having one stored in a static variable.}
- Static members are typically accessed with the class name and the dot operator, e.g.
Math.PI
and Math.abs()
.
- When in doubt make methods non-
static
. However a few good reasons to write static
methods are: 1) Utility methods that are pure functions like the ones in the Math
class, particularly if all the methods in the class are that kind of method. 2) Methods used to obtain instances of the class that do things constructors can't such as sometimes constructing a new object and sometimes not or to have several static methods that take the same types of arguments but construct objects in different ways.