Here is an example of a class in java. Underneath, we will go through the class line by line to review / explain the important terms.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Ball {
private int radius;
private String color;
private String type;
private static final String SHAPE = "spherical";
public Ball() {
radius = 10;
color = "orange";
type = "basketball";
}
public Ball(int initialRadius, String initialColor, String initialType) {
radius = initialRadius;
color = intialColor;
type = intialType;
}
public int getRadius() {
return radius;
}
public double getVolume() {
// volume of a sphere is 4/3 pi r^3
return 4.0 / 3 * 3.14159 * radius * radius * radius;
}
public void print() {
System.out.println("I am a " + color + " " + type+ ".");
System.out.println("My radius is " + radius + " cm.");
}
public void setColor(String newColor) {
color = newColor;
}
public void growBy(int growAmount) {
radius = radius + grow;
}
}
Let’s go through this a little bit of a time, defining concepts as we go
The line
1
class Ball {
tells us we are defining a class called Ball
that will be used as a blueprint to create many different balls.
When we create an actual ball, we call that an instance of the ball class.
The next three lines
1
2
3
private int radius;
private String color;
private String type;
define the instance variables of the class. These three values represent the attributes that every ball has that might be different between various instances. You will also see these called fields in some places. Every single ball that is created using this class will have their own copy of each of these variables, so they belong to the instance. For example, ball1.radius
and ball2.radius
might have different values.
These values are preceded by the word private
to let us know that these attributes are personal to the class - only methods inside the class can direclty read and edit these variables. This is standard practice in java.
The next line
1
public static final String SHAPE = "spherical";
defines a constant that belongs to the entire class Ball
. Every ball is spherical, so this value does not belong to a single instance, it belongs to the entire class, and it cannot change.
Technically static tells us the variable belongs to the class while final tells us the variable cannot be edited, but in practice you will nearly always see them together when used in a class this way. This one is public so that we know it can be accessed anywhere that has access to the Ball class.
The next several lines define two constructors, which are special pieces of code that run when an object is created.
1
2
3
4
5
public Ball() {
radius = 10;
color = "orange";
type = "basketball";
}
The constructor above can be used to create, essentially, a default Ball
. If another piece of code somewhere uses the command Ball b1 = new Ball()
, then b1
will become a 10-cm radius orange basketball, the default Ball object.
The signature of this constructor is Ball()
. That tells java that this is a constructor (because it has the same name as the class) and that it does not take any arguments (because the parentheses are empty)
The constructor is preceded by the word public
to let us know that we will be able to use this constructor to make Ball
objects anywhere that can see the class.
1
2
3
4
5
public Ball(int initialRadius, String initialColor, String initialType) {
radius = initialRadius;
color = intialColor;
type = intialType;
}
This code allows the user to create a new ball to their own specification. By using the command Ball b2 = new Ball(15, "black and white", "soccer")
, b2
can be created as a 15-cm radius black and white soccer ball.
The three values that are inside the parentheses are called the parameters of the method, or the arguments passed to the constructor. You usually see those words used interchangeably.
The signature of this constructor is Ball(int, String, String)
. This tells java that ths defines a constructor, and that it needs to be followed by an integer and two strings.
We have just seen an example of overloading in java. When two methods (or constructors) have the same name but different signatures, that is called overloading. Not every programming language allows this - JavaScript, for example, does not - but most class-based languages do.
When you have an overloaded method, java uses the *signature (as seen in the above examples) to decide which method to use. For this reason, you can have as many methods with the same name as you want as long as they call for different types or use a different order in the parameters, but simply changing the names of the parameters is not enough.
For example, the following code is NOT allowed:
1
2
3
4
5
6
public Ball(int size){
//whatever
}
public Ball(int weight){
//whatever
}
A human may read this code and think they are defining different things, but if a user types the code Ball b3 = new Ball(10)
, how would java know which one to use? Is the 10 supposed to be the weight or the height? It won’t know, because those two methods have the same signature, Ball(int)
.
However, the following code is allowed:
1
2
3
4
5
6
public Ball(int size) {
//whatever
}
public Ball(double weight) {
//whatever
}
because java will know if we type Ball b2 = new Ball(12)
we mean the first one, since 12 is an int, but if we type Ball b2 = new Ball(12.0)
we mean the second one, since 12.0 is a double. This is confusing but not illegal.
A method is a behavior, action, or question that is performed by or to an object or a class. The rest of our code defines the instance methods for balls, or actions that can be performed by or on the individual instances. Generally, when we just say the word method we mean an instance method, because they are the most common type of methods. Technically, constructors are a method as well.
These lines define two different non-void methods. A non-void method is any method that can be thought of as being a question you ask the object, to which an answer is expected.
1
2
3
4
5
6
7
8
9
public int getRadius() {
return radius;
}
public double getVolume() {
// volume of a sphere is 4/3 pi r^3
double vol = 4.0 / 3 * 3.14159 * radius * radius * radius;
return vol;
}
The first method has the signature getRadius()
. The int
in front of the name tells us that this method has an answer that is of type int - that is, when we use this method we are essentially asking the ball a question, and it will respond with an integer. We could USE this method anywhere with a line like int d = ball1.getRadius()
. Notice the parentheses! That is how java knows you are trying to access a method and not an instance variable.
When you have a method that returns an instance variable directly, we call that method a getter or, more formally, an accessor. They generally, by convention, start with the word get
.
The next method has the signature getVolume()
. Notice that it returns a double
but is otherwise similar to the radius on the outside. On the inside, it’s doing something more complicated - rather than simply returning the instance variable, it has to calculate the volume first! For that reason it is not technically an accessor, though it works much like one.
One important thing to realize is that somebody using this code might not know that getRadius()
is a basic accessor while getVolume()
is more complex. This allows the person writing the Ball
class to change how these work later, if they need to, without breaking any code that already uses those methods.
The final method in this class are void methods. These methods are not questions that have an answer - they are actions, and only actions.
1
2
3
4
5
6
7
8
9
10
11
12
public void print() {
System.out.println("I am a " + color + " " + type+ ".");
System.out.println("My radius is " + radius + " cm.");
}
public void setColor(String newColor) {
color = newColor;
}
public void growBy(int growAmount) {
radius = radius + grow;
}
The first method, print()
is a void method with no arguments. It simply tells the ball to print a sentence about itself. A user could use this simply by running ball1.print()
or ball2.print()
The second method, setColor(String)
, is used to change the color of the ball. This is called a setter or mutator because it sets the value of the parameter, mutating (changing) the object itself. A user could use code like ball1.setColor("green")
to change ball1 to being a green object.
The final method, growBy(int)
tells the ball to grow by a certain amount. A user could use ball2.growBy(2)
to make ball2 grow two inches larger.
None of these methods require a response - they are simply done. One of them, print()
doesn’t even require any arguments.