Intro
The main points about classes and objects so far:
- A
classis a blueprint for creating an instance or object. - Objects have state and behavior - fields and methods.
- A
classcan havestaticor non-static(instance) fields and methods. staticfields and methods belong to the blueprint.- Each object gets its own copy of an instance field.
- We can call instance methods on an object using the
.operator. - Create an instance with the
newoperator. - Use a reference variable to point to an instance.
A class (blueprint) defining state (fields) and behavior (methods) for objects that model a sphere:
public class Sphere3 {
public static double PI = 3.14159; // static field
public double radius; // instance field
// instance method
public double getVolume() {
double vol = 4.0 / 3.0 * PI * radius * radius * radius;
return vol;
}
// static method
public static double calculateCircumference(double r) {
return PI * 2 * r;
}
}
Below are examples of creating and using a Sphere3 object, and using the Sphere3 static method.
Sphere3 sphere = new Sphere3(); // reference points to new instance
sphere.radius = 12.0; // use reference to access fields and methods
double vol = sphere.getVolume();
double circum = Sphere3.calculateCircumference(15.0); // static method call
// using class name