πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

πŸ“±

2.1 Objects: Instances of Classes

3 min readβ€’december 29, 2022

Milo Chang

Milo Chang

Milo Chang

Milo Chang


AP Computer Science AΒ πŸ’»

130Β resources
See Units

Java is anΒ object-orientated programming language, which means that most major programs in Java are about the manipulation ofΒ objects.

What are Objects?

Objects are aΒ reference type,Β which refers to how they are combinations of other primitive and reference data types. When you refer to them, you are not referring to the actual object itself, but where it is stored in data.
How do we know what combination of primitive and reference types each object has? This is due to the help of aΒ class.

What is a Class?

A class is like a template that defines what an object is like and what the object can do. A class is basically the guidelines for a type of object, and an object is a particular instance of that class.

Objects are Instances of Classes

We can think of a class as a blueprint for a house, and the object is a particular house. Different houses are different objects. The different houses may look different, but they have the same general features and functions. This is how classes and objects work.

Real-world Example

Here's another analogy that might help you better understand the relationship between objects and classes. We could create a Student class in Java that defines the characteristics that any student would have:
public class Student {
// instance variables
String name;
int age;
double gpa;
// constructor
public Student(String name, int age, double gpa) {
this.name = name;
this.age = age;
this.gpa = gpa;
}
}
Don't worry if you don't know what a constructor is yet, since that will be introduced in the next topic and then we'll delve deeper in Unit 5. For now, just focus on the fact that the Student class has instance variables for a student's name, age, and GPA.
The Student class above is like a blueprint. Every student that will exist in our program should have a name, age, and GPA.
Now we'll create actual students using these blueprints.
Student alice = new Student("Alice", 18, 3.5);
Student bob = new Student("Bob", 20, 3.0);
This will create two students, one called alice and the other called bob. When we initialize the two students, we set their names, ages, and GPAs.
Now we can print out information about each student.
System.out.println(alice.name); // prints "Alice"
System.out.println(alice.age); // prints 18
System.out.println(alice.gpa); // prints 3.5
System.out.println(bob.name); // prints "Bob"
System.out.println(bob.age); // prints 20
System.out.println(bob.gpa); // prints 3.0
If we tried to print out something like Student.gpa, we would get an error message. Imagine if someone asked you to give them the GPA of a student. If you didn't know which student they were talking about, that would be impossible. When we ask for bob.gpa, we are asking for Bob's GPA. But if we ask for Student.gpa, the computer has no idea what we actually want.
In this example, the Student class tells you what characteristics an instance of the Student class should have should have. We then create two instances of the Student class; in the context of our example, this means we create two students. alice and bob are objects. They represent specific students, one named Alice and one named Bob.
As you go through the rest of this unit, you'll learn more about using objects, which should help you better understand the relationship between classes and objects.
Browse Study Guides By Unit
βž•Unit 1 – Primitive Types
πŸ–₯Unit 3 – Boolean Expressions & if Statements
πŸ•ΉUnit 4 – Iteration
βš™οΈUnit 5 – Writing Classes
⌚️Unit 6 – Array
πŸ’ΎUnit 7 – ArrayList
πŸ’»Unit 8 – 2D Array
πŸ–²Unit 9 – Inheritance
πŸ–±Unit 10 – Recursion
πŸ™Exam Reviews