Objects in Java have a lot in common with things we can see in the real world. Objects may be anything from a kitten to a lighter to a pen to an automobile.
There are three distinguishing qualities of them:
Identity State Behavior
The color, size, gender, and age of a cat are all characteristics of its state, whereas napping, purring, meowing for food, or racing around like mad at 4 in the morning are all examples of its behavior.
An object’s identification is its distinguishing feature, such as a random ID number or a memory location. Although a lighter only has two distinct states and actions (on and off), it nonetheless has a unique identification (the manufacturer ID).
The many states of a Java object are kept in fields that record the object’s attributes. A handgun in a first-person shooter game with an eight-bullet clip has nine possible states: loaded (with eight bullets), partially loaded (with seven bullets), partially empty (with zero bullets), and unloaded (with no bullets).
Methods that modify the object’s internal state make its behavior available. When the player fires their handgun, the “shooting” action will update the state from “8 bullets” to “7 bullets” and so on.
The “reloading” action will reset the handgun to its factory settings of “8 bullets.”
A Java object may be made in three stages:
Creation of the object instance.
Object creation and initialization.
When an object is defined in Java, it is given a name. Memory is allocated by instantiating the object. When an object is initialized, its attributes have their default values set. Unlike class properties, which only affect instances of a given class, object properties are shared by all instances of a given class.
Java object characteristics
Objects often allow interaction through their means. Objects maintain authority over their usage by the outside world by means of state and method changes.
For this reason, if you try to pass in a number larger than “8 bullets,” the procedure will throw an exception. Data encapsulation is the technical term for this practice.
Separate objects may be created from the code, and each object’s source code can be developed and updated separately. If an item in a program is causing problems, it is simple to delete it and try again.
The following syntax is used to construct a new “tree” object, denoted by the letter t: In this expression, a new Tree t = new Tree().