Javatpoint Logo
Javatpoint Logo

Object in Java

Java is an object-oriented programming language, which means that objects play a central role in its design. Fundamental things in Java that contain data and behaviours are called objects. For Java code to be efficient and modular, understanding objects is crucial. We will examine objects in this article, including what they are, how they are made, and how Java objects interact with one another.

What is an Object?

An object in Java is an instance of a class. A class is a model or a template that specifies the composition and operation of objects. It describes the possible data and behaviours that an object of that class can have in terms of properties and methods. Consider a class as the design for a house, and an object as the actual house constructed from that design.

Creating Objects in Java

In Java, the new keyword must be used in conjunction with the class name and brackets to create an object. If the class constructor calls for them, the brackets may be filled with parameters or left empty. When an object is formed, a particular method called the constructor is called, and it is in charge of initialising the object's state.

Here is an illustration of how to make an object of the class "Person":

In this example, we create a new object of the Person class and assign it to the variable person.

Object State and Behavior

State and behaviour are the two main components of objects. An object's state or properties are represented by its data, while its behaviour is a representation of the actions or operations the object is capable of.

Instance variables, usually referred to as fields, are used to specify the state. These variables hold the information connected to an object. To express the state of a person object, for instance, a Person class might have instance variables for name, age, and address.

Methods determine behaviour. The functions known as methods carry out particular actions on an object or give access to its data. To change the state or obtain data for an object, a Person class can have methods like getName(), getAge(), and setAddress().

Object Interactions

By using each other's methods or data, objects communicate with one another. The ability to create complex systems is enabled by this interaction, which is at the core of object-oriented programming.

Filename: Car.java

Output:

Brand: ToyotaModel: Camry
Year: 2020
The Toyota Camry engine has started.

In this illustration, a car object is represented by the Car class. In addition to constructor, getter, and setter methods for accessing and changing the object's properties, it also provides the methods startEngine and stopEngine for taking actions on the car object. Private instance variables for the brand, model, and year are also included. The main method explains how to create, access, change, and call the methods of a Car object.

Conclusion

The fundamental units of a Java programme are objects. They provide a modular and structured approach to programming by encapsulating data and behaviour. You may use the power of object-oriented programming to construct reliable and maintainable Java applications by knowing how to create objects, define their state and behaviour, and create interactions between them.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA