Skip to main content
Logo image

Section 8.1 Anatomy of a class

In Sectionย 3.2ย APIs and Libraries we saw how classes define new data types whose values share a set of attributes and behaviors. We also saw how different objects, while having the same abstract attributes, each have their own values for those attributes and how an objectโ€™s values can then affect their behavior in some way. For instance all Turtle objects have a position but two Turtle objects can be at different positions on the screen. And both can be made to move forward from wherever they are with the forward() method.
To review the vocabulary, a class defines the attributes and behaviors that all objects of that class will have. From a class we make many objects also called instances of the class. Each instance has its own values for the attributes defined in the class. Attributes are the data or properties that an object knows about itself, for example, a turtle objectโ€™s color and size. Behaviors are the things that an object can do. For example, a turtle object can go forward, turn left, or turn right. The attributes and behaviors of a class are defined in the class, but each object has its own values for the attributes.
Figure 8.1.1. Using a cookie cutter (class) to make cookies (objects)
The following picture has lots of cats (objects of the type cat). They are all different, but they share the same attributes and behaviors that make up a cat. They are all instances of cat with different values for their attributes. Name some of the attributes and behaviors of the cats below. For example, the color ( attribute) of the first cat is black ( attribute value) and it is playing ( behavior).
Figure 8.1.2. Pictures of cats (cat objects)
But thinking in terms of abstract attributes and behaviors is the outside view of a class, for when weโ€™re using an API. Now itโ€™s time to open up the black box and see how classes are structured on the inside. In this section weโ€™ll take a very quick tour through the main parts of a class. Then weโ€™ll go through each part in more detail in the following sections.

Subsection 8.1.1 Quick tour: instance variables, constructors, and methods

When we open up the black box, we find classes have three main parts: instance variables, constructors, and methods. In order to have something to talk about, letโ€™s look at a simple but complete class, Person.
The purpose of this class is to model a person as someone who has two attributes, a name and an age. A person also has two behaviors: they can print out a greeting where they introduce themselves and they can answer the boolean question of whether they can vote, which depends on whether they are eighteen or older.
For instance we should be able to write this code:
Person person1 = new Person("Joe", 18);
Person person2 = new person("Sally", 17);
person.greet(person2);
System.out.println("Joe can vote: " + person1.canVote());
System.out.println("Sally can vote: " + person2.canVote());
and when we run it get this output:
Hello Sally, I'm Joe.
Joe can vote: true
Sally can vote: false
Hereโ€™s the code for a Person class that meets those requirements.
public class Person {

  // Instance variables
  private String name;
  private int age;

  // Constructor
  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }

  // Methods
  public void greet(Person other) {
    System.out.println("Hello " + other.name + ", I'm " + name + ".");
  }

  public boolean canVote() {
    return age >= 18;
  }
}
Letโ€™s take this in chunks, starting with the first line:
public class Person {
Almost all the classes we write will start with the keyword public. It just means the class can be used in other classes. We will occasionally have to write classes without the public in exercises in this book but only because of limitations of the platform this book runs on. However, the really important word in this line is class which says weโ€™re defining a class.
Next is the name of the class, Person. Class names in Java are always capitalized. It is not strictly required by the language but is as universally followed a convention as just about anything in programming. Consequenly, Java code with uncapitalized class names can be extremely confusing, especially to experienced Java programmers, so donโ€™t write code with uncapitalized class names!
After the name of the class comes the opening { of the class body which, like the body of a method, is defined inside a pair of {}s. Everything that makes up the definition of the class must be inside that pair of {}.
The first thing in the body of this class are two variable declarations:
private String name;
private int age;
The only new thing here, compared to other variable declarations weโ€™ve seen, is the keyword private. After that, they look like regular variable declarations with a type and a name. But because these variables are defined at the top level of the class, meaning not inside a method, they are called instance variables and they are what define the attributes instances of this class have. So from these two lines we can see that every Person object has two attributes, a name and an age.
These variables define the structure of a Person object and they are called instance variables because each instance of the class, that is each object, has its own set of variables that arenโ€™t shared with other instances.
The next thing we define in a class are usually its constructors. Weโ€™ll talk a lot more about constructors in a future section but the short version is, a constructorโ€™s job is to initialize an objectโ€™s instance variables. Our Person class has one constructor:
public Person(String name, int age) {
    this.name = name;
    this.age = age;
}
Weโ€™ll get into the details soon but for now, just know that this code runs we write something like new Person("Joe", 18) and the arguments "Joe" and 18 become the values of the parameters name and age. Those values are then assigned to this.name and this.age which are specifically the instance variables we defined above. This constructor guarantees that whenever we make a Person we have to supply a name and an age and those values are stored in the objectโ€™s intstance variables.
Finally the behaviors of this class, emitting a greeting, and answering the question of whether a specific Person can vote, are provided by the two methods:
public void greet(Person other) {
  System.out.println("Hello " + other.name + ", I'm " + name + ".");
}

public boolean canVote() {
  return age >= 18;
}
You should be familiar with how methods work from Sectionย 3.1ย Writing methods but there are two new things here. First, both of these methods refer to a variable that is neither a parameter nor is declared inside the method. The method greet has one parameter, to, but it also refers to the variable name. And canVote has no parameters but refers to the variable age.
This works because of the second new thing that is that these methods do not have the keyword static in their signature. That makes them instance methods which means they have access to the instance variables defined in the class.
Watch this short video to review some of the key vocabulary of object-oriented programming and then weโ€™ll move on to a more detailed examination of these three parts of a class.
Watch the following video by Dr. Colleen Lewis about classes and objects:
Figure 8.1.3. Classes and Objects

Activity 8.1.1.

Activity 8.1.2.

How many objects can you create from a class in Java?
  • There is one definition of a class, but the class can create as many objects as are needed.
  • There is no limit on the number of objects you can create from a class.
  • There is no limit on the number of objects you can create from a class.
  • As many as you need
  • You can create as many objects as you need from one class.

Activity 8.1.3.

What specifies the behavior for objects of a class in Java?
  • attributes
  • attributes specify the data that an object keeps track of.
  • methods
  • Methods specify the behavior of all objects of a class.
  • class
  • While the class does specify the behavior of all objects created by that class, what part of a class specifies the behavior?
  • object
  • The object behavior is specified by the methods in the class that created the object.

Activity 8.1.4.

What are the data or properties of an object called?
  • attributes
  • attributes specify the data that an object keeps track of.
  • methods
  • Methods specify the behavior of all objects of a class.
  • class
  • While the class does specify the data that all objects of the class keep track of, what part of the class stores the data?
  • object
  • The object data is stored in the attributes of the object. The attributes are defined in the class.
You have attempted of activities on this page.