Fiveable
Fiveable
pep
Fiveable
Fiveable

or

Log in

Find what you need to study


Light

2.1 Objects: Instances of Classes

3 min readdecember 28, 2022

Milo Chang

Milo Chang

Milo Chang

Milo Chang

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.

Key Terms to Review (8)

Class

: A class is a blueprint or template for creating objects in object-oriented programming. It defines the properties and behaviors that an object of that class will have.

Constructor

: A constructor is a special method within a class that is used to initialize objects of that class. It is called automatically when an object is created and helps set initial values for its attributes.

Instance Variables

: Instance variables are variables declared within a class but outside any method. They hold unique values for each instance (object) of the class and define the state or characteristics of an object.

Object-Oriented Programming

: Object-oriented programming is a programming paradigm that organizes code into objects, which are instances of classes. It focuses on creating reusable and modular code by encapsulating data and behavior together.

Objects

: Objects are instances of a class that represent real-world entities or concepts. They encapsulate data (attributes) and behavior (methods) into a single entity.

Primitive data types

: Primitive data types are basic data types that are built into a programming language and represent simple values. They include integers, floating-point numbers, characters, booleans, and more.

Reference Type

: A reference type is a data type that stores the memory address of an object rather than the actual value. It allows multiple variables to refer to the same object, and changes made to one variable will affect all other variables referencing that object.

System.out.println

: System.out.println is a Java statement used to display output on the console. It prints the specified message or value and adds a new line character at the end.

2.1 Objects: Instances of Classes

3 min readdecember 28, 2022

Milo Chang

Milo Chang

Milo Chang

Milo Chang

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.

Key Terms to Review (8)

Class

: A class is a blueprint or template for creating objects in object-oriented programming. It defines the properties and behaviors that an object of that class will have.

Constructor

: A constructor is a special method within a class that is used to initialize objects of that class. It is called automatically when an object is created and helps set initial values for its attributes.

Instance Variables

: Instance variables are variables declared within a class but outside any method. They hold unique values for each instance (object) of the class and define the state or characteristics of an object.

Object-Oriented Programming

: Object-oriented programming is a programming paradigm that organizes code into objects, which are instances of classes. It focuses on creating reusable and modular code by encapsulating data and behavior together.

Objects

: Objects are instances of a class that represent real-world entities or concepts. They encapsulate data (attributes) and behavior (methods) into a single entity.

Primitive data types

: Primitive data types are basic data types that are built into a programming language and represent simple values. They include integers, floating-point numbers, characters, booleans, and more.

Reference Type

: A reference type is a data type that stores the memory address of an object rather than the actual value. It allows multiple variables to refer to the same object, and changes made to one variable will affect all other variables referencing that object.

System.out.println

: System.out.println is a Java statement used to display output on the console. It prints the specified message or value and adds a new line character at the end.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.