How to define a class that you can use to construct Circle objects:
public class Circle
{
public double radius = 1; /** The radius of this circle */
public Circle() { } /** Constructor 1 for a circle object */
public Circle(double newRadius) /** Constructor 2 for a circle object */
{
radius = newRadius;
}
public double getArea() /** Return the area of this circle */
{
return 3.14159 * radius * radius;
}
public void setRadius(double newRadius) /** Set new radius for this circle */
{
radius = newRadius;
}
}
|
We can use the Circle class to create 2 Circle objects as follows:
public static void main()
{
Circle circle1 = new Circle();
// Invokes Circle() to make this circle
Circle circle2 = new Circle(2);
// Invokes Circle(double) to make this circle
}
|
DEMO: demo/03-classes/01-class - in BlueJ - show the objects in main(). Step into the constructors
The Unified Modeling Language representation of classes and objects:
public static void main()
{
Circle circle1 = new Circle();
// Invokes Circle() to make this circle
Circle circle2 = new Circle(2);
// Invokes Circle(double) to make this circle
}
|
A Java class uses variables to define data fields (properties) of objects:
public class Circle
{
public double radius = 1; /** The radius of this circle */
public Circle() /** Constructor 1 for a circle object */
{
}
public Circle(double newRadius) /** Constructor 2 for a circle object */
{
radius = newRadius;
}
public double getArea() /** Return the area of this circle */
{
return 3.14159 * radius * radius;
}
public void setRadius(double newRadius) /** Set new radius for this circle */
{
radius = newRadius;
}
}
|
A Java class uses methods to define the actions/behaviors of objects:
public class Circle
{
public double radius = 1; /** The radius of this circle */
public Circle() /** Constructor 1 for a circle object */
{
}
public Circle(double newRadius) /** Constructor 2 for a circle object */
{
radius = newRadius;
}
public double getArea() /** Return the area of this circle */
{
return 3.14159 * radius * radius;
}
public void setRadius(double newRadius) /** Set new radius for this circle */
{
radius = newRadius;
}
}
|
Important note: methods to define the actions of objects do not have the static qualifier
public class Circle
{
public double radius = 1; /** The radius of this circle */
public Circle() /** Constructor 1 for a circle object */
{
}
public Circle(double newRadius) /** Constructor 2 for a circle object */
{
radius = newRadius;
}
public double getArea() /** Do NOT use static qualifier ! */
{
return 3.14159 * radius * radius;
}
public void setRadius(double newRadius) /** Do NOT use static qualifier ! */
{
radius = newRadius;
}
}
|
A class provides special methods called constructors which are invoked (only) to create a new object:
public class Circle
{
public double radius = 1; /** The radius of this circle */
public Circle() /** Constructor 1 for a circle object */
{
}
public Circle(double newRadius) /** Constructor 2 for a circle object */
{
radius = newRadius;
}
public double getArea() /** Return the area of this circle */
{
return 3.14159 * radius * radius;
}
public void setRadius(double newRadius) /** Set new radius for this circle */
{
radius = newRadius;
}
}
|
Constructors are designed to perform initializing actions, such as initializing the data fields of objects
A class that represents real world objects usually does not need a main( ) method:
public class Circle
{
public double radius = 1; /** The radius of this circle */
public Circle() // called when you use: new Circle()
{
}
public Circle(double newRadius) // called when you use: new Circle(number)
{
radius = newRadius;
}
public double getArea() /** Return the area of this circle */
{
return 3.14159 * radius * radius;
}
public void setRadius(double newRadius) /** Set new radius for this circle */
{
radius = newRadius;
}
}
|
Without a main( ) method, such class cannot be run as a Java program
You may put a main( ) method in the Circle class to test the methods:
public class Circle
{
public double radius = 1; /** The radius of this circle */
public static void main(String[] args)
{
Circle circle1 = new Circle();
Circle circle2 = new Circle(2);
}
public Circle() { } // called when you use: new Circle()
public Circle(double newRadius) // called when you use: new Circle(number)
{
radius = newRadius;
}
public double getArea() /** Return the area of this circle */
{
return 3.14159 * radius * radius;
}
public void setRadius(double newRadius) /** Set new radius for this circle */
{
radius = newRadius;
}
}
|
DEMO: demo/03-classes/02-constructor --- I prefer to write a separate class to do the testing...
Example invoking methods on objects:
public static void main()
{
Circle circle1 = new Circle(); // Create a Circle object circle1
Circle circle2 = new Circle(2); // Create a Circle object circle2
double area1 = circle1.getArea(); // Tell circle1 to run getArea()
System.out.println("Area of circle1 = " + area1);
double area2 = circle2.getArea(); // Tell circle2 to run getArea()
System.out.println("Area of circle2 = " + area2);
circle1.setRadius(5); // Tell circle1 to run setRadius()
area1 = circle1.getArea(); // Tell circle1 to run getArea()
System.out.println("Area of circle1 = " + area1);
}
|
DEMO: demo/03-classes/03-invoke-method
The Circle class implementation allows a user to access the object variables directly:
public static void main()
{
Circle circle1 = new Circle(); // Create a Circle object circle1
double area1 = circle1.getArea(); // Tell circle1 to run getArea()
System.out.println("Area of circle1 = " + area1);
circle1.radius = 10; // Update the radius variable directly
area1 = circle1.getArea(); // Tell circle1 to run getArea()
System.out.println("Area of circle1 = " + area1);
}
|
Because:
public class Circle
{
public double radius = 1; // The radius variable is not private
....
|
We can prevent direct access to variables in a class by using the private qualifier:
public static void main()
{
Circle circle1 = new Circle(); // Create a Circle object circle1
double area1 = circle1.getArea(); // Tell circle1 to run getArea()
System.out.println("Area of circle1 = " + area1);
circle1.radius = 10; // Compile error !!!
area1 = circle1.getArea(); // Tell circle1 to run getArea()
System.out.println("Area of circle1 = " + area1);
}
|
Because:
public class Circle
{
private double radius = 1; // Disallows direct access to radius
....
|
DEMO: demo/03-classes/04-private
The following diagrams show what happens inside the computer system:
Circle circle1; // circle1 is a reference variable !
circle1 = new Circle(4);
|
"Circle circle1" will allocate (reserve memory) a reference variable circle1:
The following diagrams show what happens inside the computer system:
Circle circle1; // circle1 is a reference variable !
circle1 = new Circle(4);
|
"new Circle(4)" will allocate (reserve memory) for an radius variable and return its base address:
The radius variable will also be initialize with the value 4.
The following diagrams show what happens inside the computer system:
Circle circle1; // circle1 is a reference variable !
circle1 = new Circle(4);
|
"circle1 = " will assign the return value to the variable circle1:
Summary of the effect of:
Circle circle1; // circle1 is a reference variable !
circle1 = new Circle(4);
|
The reference variable circle1 will reference to a newly created Circle object:
Previously, we used this expression to access the radius variable in Circle1:
circle1.radius = 10; |
In this diagram, you can clearly see that Java uses the reference variable Circle to access radius: