1. Using Objects and Methods

Objects: Instances Of Classes

Objects: Instances of Classes

students, in AP Computer Science A, objects are one of the most important ideas in Java πŸ’‘. A class gives the blueprint, and an object is a real instance built from that blueprint. If you understand this relationship, you can read Java code more clearly, write methods that work with objects, and explain how programs model real-world situations.

Lesson objectives

By the end of this lesson, you should be able to:

  • Explain what a class and an object are
  • Describe how an object is created from a class
  • Identify instance variables and instance methods
  • Use object references correctly in Java code
  • Connect objects to the larger topic of using objects and methods

Quick hook

Think about a class like a cookie cutter and an object like one cookie πŸͺ. The cutter is not the cookie itself. It is the design used to make many cookies. In Java, a class is the design, and each object is a separate created thing that can store its own data and do its own actions.

What a class is and what an object is

A class is a blueprint or template for creating objects. It defines what data an object will store and what behaviors it can perform. For example, a Dog class might describe that every dog has a name, breed, and age, and that every dog can bark or eat.

An object is an instance of a class. That means it is a specific, real thing made using the class blueprint. If Dog is the class, then one object could be a dog named Buddy, and another could be a dog named Luna. Both are dogs, but they are different objects with their own values.

In Java, objects are created with the new keyword. For example:

$$\texttt{Dog myDog = new Dog();}$$

This line creates a new Dog object and stores a reference to it in myDog. The variable myDog does not contain the whole object itself. Instead, it holds a reference, which is like an address pointing to where the object lives in memory.

This idea is important because many students accidentally think a variable like myDog is the actual object. In Java, object variables usually store references, not the object data directly. That matters when you compare objects, pass them into methods, or assign one object variable to another.

Instance variables and instance methods

Objects are defined by the features and actions given in their class.

An instance variable is a field that belongs to each object. Each object gets its own copy of these variables. In a Student class, instance variables might include name, gradeLevel, and gpa. One student object might have name = "Mia" and another might have name = "Jordan".

An instance method is a method that works on a specific object. It often uses the object's instance variables. For example, a BankAccount object might have a method called deposit that changes the balance of that one account.

Example class idea:

$$\texttt{class BankAccount \{}$$

$$\texttt{\ \ double balance;}$$

$$\texttt{\ \ void deposit(double amount) \{}$$

$$\texttt{\ \ \ \ balance = balance + amount;}$$

$$\texttt{\ \ \}}$$

$$\texttt{\}}$$

If one BankAccount object starts with a balance of $100.00$ and another starts with a balance of $25.00$, a deposit into one account should not change the other. That is because each object has its own state.

Creating and using objects

To use an object, you often follow three steps:

  1. Declare an object reference variable
  2. Create the object with new
  3. Call methods using dot notation

For example:

$$\texttt{Scanner input = new Scanner(System.in);}$$

$$\texttt{String word = input.next();}$$

Here, input is an object reference to a Scanner object. The method call input.next() tells that specific scanner to read the next token of input.

Dot notation is the standard way to access an object's methods and fields:

$$\texttt{objectName.methodName()}$$

or, when a field is accessible:

$$\texttt{objectName.fieldName}$$

In AP Computer Science A, you must know that methods belong to objects and are called through object references. A method call may change the object's state, return a value, or both.

Real-world example

Imagine a playlist app 🎡. The class Song describes a song's title, artist, and length. Each song in the app is an object. One Song object might represent "Halo" by Beyoncé, while another might represent "Counting Stars" by OneRepublic. The app can call methods such as play() or getLength() on each song object.

References, null, and assignment

Because object variables store references, assignment works differently for objects than it does for primitives.

For primitives, the value is copied:

$$\texttt{int a = 5;}$$

$$\texttt{int b = a;}$$

Now b has its own separate value of $5$.

For objects, the reference is copied:

$$\texttt{Dog a = new Dog();}$$

$$\texttt{Dog b = a;}$$

Now a and b refer to the same object. If a method changes the object through b, the change is also visible through a, because they point to the same underlying object.

This is a major AP CSA idea because it helps explain why two variables can seem to "share" changes.

Another important value is null. A reference variable is null when it refers to no object at all. If you try to call a method on null, Java throws a NullPointerException. That is why object variables must be assigned a real object before use.

Example:

$$\texttt{String text = null;}$$

$$\texttt{text.length();}$$

This would cause an error because text does not refer to a String object.

How objects fit into methods and the larger unit

Objects are not separate from methods; they are used together constantly. Many methods take objects as parameters, return objects, or are instance methods that operate on object state.

For example, suppose a method is designed to update a Student object's grade. The method might receive a Student object, change its GPA field, and return nothing. Another method might return a String representation of an object so it can be printed.

Objects also connect to strings, arrays, APIs, and libraries. A String is an object with many useful methods like length(), substring(), and indexOf(). Classes from the Java API, such as Scanner and Random, are objects students use often in AP Computer Science A.

This is why understanding objects is part of the broader topic of Using Objects and Methods. If you can read object creation, method calls, and reference behavior, you can understand much of Java programming.

Example with a library class

The Random class can create random numbers:

$$\texttt{Random r = new Random();}$$

$$\texttt{int n = r.nextInt(6) + 1;}$$

This code simulates a six-sided die 🎲. The object r is an instance of Random, and the method nextInt(6) generates a number from $0$ to $5$. Adding $1$ shifts the result to $1$ through $6$.

How to reason about object code on the AP exam

When you see object-oriented code on the exam, ask these questions:

  • What class is being used?
  • What object is being created?
  • What does each variable refer to?
  • Which method is being called?
  • Does the method change the object's state?
  • Is the value a primitive or a reference?

If a question asks what happens after a method call, inspect the object's fields before and after the call. If two variables refer to the same object, a change through one name affects the same object through the other name.

You should also remember that method names, parameter types, and return types matter. A method call must match the method's definition. For example, nextInt() is different from nextInt(int bound), and a method that returns void does not produce a value you can store in a variable.

Conclusion

students, objects are the working pieces of Java programs. A class is the blueprint, and an object is a specific instance created from that blueprint. Each object has its own state, and instance methods let it act on that state. In AP Computer Science A, you need to understand object creation, references, method calls, and how objects interact with methods and data. Once these ideas are clear, many other topics in Java become much easier to read and reason about βœ….

Study Notes

  • A class is a blueprint; an object is an instance made from that blueprint.
  • Objects are usually created with the new keyword.
  • Object variables store references, not the object itself.
  • Each object has its own instance variables, which represent its state.
  • Instance methods operate on a specific object and may change that object's state.
  • Use dot notation to call methods: object.method().
  • Assignment with objects copies the reference, so two variables can refer to the same object.
  • null means a reference points to no object.
  • Calling a method on null causes a NullPointerException.
  • String, Scanner, and Random are common Java objects used in AP Computer Science A.
  • Understanding objects is essential for reasoning about methods, libraries, and program behavior.

Practice Quiz

5 questions to test your understanding