Objects are at the heart of object-oriented programming, and they form the core of the Java language. You've already been lightly introduced to objects, but now it is time to dive in!
How are Objects Used in Java?
Objects are used to store data, act on that data, and interact with other objects. You have 100% freedom to create whatever objects you choose.
Wanna make a Cat object? Go for it. How about a City object? You got it. Modeling a Poker game? No problem!
The structure of an object is defined in a Java class. When an object is created from a class, the class is the object's data type, and the class name is the object's data type name. So, as mentioned before, there are not only primitive data types but also object data types.
How to Create an Object Type?
You can create any type of class and object you require using the new keyword. Then, you can reference these object variables just like you would reference a variable of a primitive data type.
Each time you create an object (aka an instance) of a class, you are creating an object that contains its own copies of each of the instance variables found in the class. For example, each instance (or object) of the Person has its own instance variables age, height, and name.
Java Object Example
Take the follow Person class for example:
public class Person {
public int age;
public double height;
public String name;
}
To create an object of the Person class, you use the new keyword to initialize a "new" object of a given class.
/* declaration and instantiation of an object on one line */
Person somePerson = new Person();
/* or declare on one line and instantiate on the next */
Person someOtherPerson;
someOtherPerson = new Person();
The somePerson and someOtherPerson objects are now an instance of the Person class, and each of them can/will hold their own unique values of their instance variables defined in the Person class.
How to Add Instance Variables
Now that the Person class has been created, you can add variables to them.
somePerson.age = 36;
somePerson.height = 72.2;
somePerson.name = "Jose"
someOtherPerson.age = 33;
someOtherPerson.height = 63.3;
someOtherPerson.name = "Maria"
Now, since each object has its own copies of the instance variables, you're able to set them to hold distinct values. You can now access these values anytime you want. For instance:
System.out.println("The first person is named "
+ somePerson.name + ", he/she is "
+ somePerson.age + " years old and he/she is "
+ somePerson.height + " inches tall");
System.out.println("The second person is named "
+ someOtherPerson.name + ", he/she is "
+ someOtherPerson.age + " years old and he/she is "
+ someOtherPerson.height + " inches tall");```
/* Ouput:
The first person is named Jose, he/she is 36 years old and he/she is 72.0 inches tall
The second person is named Maria, he/she is 33 years old and he/she is 63.0 inches tall
Experiment with Java Objects
In the code editor below, please demonstrate how to:
- create two unique Object of the Person class
- set the object's instance vars
- print out the name and age of both Person objects
- use the objects to get and set values
class Main {
public static void main(String[] args) {
// write your code below this line
// keep your code above this line
}
}
class Person {
public String name;
public int age;
}
Summary: Java Objects
- Objects are used to store data, act on data, and interact with other objects
- Classes define Object data types
- You can think of classes as blueprints for object - from a single class, you can create many objects
- Each instance of a class, known as an "object" of that class, has its own values for the given instance variables
- Objects are created by using the
newkeyword