- Published: September 10, 2022
- Updated: September 10, 2022
- University / College: Baylor University
- Language: English
- Downloads: 19
Objects (Instances)
Objects are the building blocks of the Java programming language being an object-oriented language. An object is just like real-world objects and their description and properties are described in the object class. An object can be composed of several other objects. The limitations of the properties or the description of an object is based on its class descriptions, its methods and properties set.
An instance of an object is one specific occurrence of the specific object where all the properties and behavior of the object class are inherited. An object reference called the instance of the object is returned when the java object constructor is called. One the object constructor is called, an instance of the object it represents is returned. Instances of objects interact with other objects by invoking their respective methods. Instances of objects are can be created and destroyed while the program runs
Consider the following code:
Bicycle{
}
public Bicycle {
int speed = 30;
}
public void getSpeed{
return speed();
an instance of the class bicycle can be created by calling the constructor of the object
Bicycle AlexBicycle = new Bicycle();
Bicycle MyrnaBicycle = new bicycle();
In the code above, two instances of the class Bicycle are created. They have the same properties but they can have different values. For example if the following code is added
MyrnaBicycle. getSpeed = 20;
AlexBicycle. getSpeed = 30;
sets the speed of MyrnaBicycle to 20 and AlexBicycle to 30.
Since objects and object instantiation are one of the main features of object-oriented programming it just reflects that the the benefits of using object-oriented programming are also aligned in the benefits of using Object instances. One of the major benefits is its reuse capability. You can create several instances of a class in a single program without creating additional code for the object class. Automatically, all the properties of the class are inherited by each instance of the class. This also follows that using objects instances is very maintainable since if one wants to set new propertied of a class then only one code needs to be changed even of there are hundred of instances of the object.
References:
Objects, Instance Methods, and Instance Variables. Retrieved from http://math. hws. edu/javanotes/c5/s1. html