=== Video link ===
Access Modifiers In Java : • Access Modifiers in Java || Private, Publi...
Object is a bundle of data and its behaviour(often known as methods)
An object in Java is any real-world things that have properties and actions.
In other words, an entity that has state and behavior is known as an object.
Here, state represents properties and behavior represents actions or functionality.
=== Characteristics of an object in Java ===
** An object has three characteristics **
1. State: State represents properties of an object. It is represented by instance variable/attribute of an object.
The properties of an object are important because the outcome of functions depend on the properties.
2. Behavior: Behavior represents functionality or actions. It is represented by methods in Java.
3. Identity: Identity represents the unique name of an object. It differentiates one object from the other.
The unique name of an object is used to identify the object.
=== How to Create Object in Java ===
In Java, an object of a class is created using the new keyword in three steps. They are as follows.
1. Declaration of a reference variable.
2. Creation of an object.
3. Linking the object and the reference variable.
=== Declaration of Reference variable ===
Basically, the declaration of an object means refers to an object. A general form of declaration of a reference variable is given below.
Classname object_reference_variable; // Creating a reference variable of type Class.
The Classname is the name of the class which is being instantiated. The object reference variable is a variable of type Classname.
=== Creating Object in Java ===
We can create an object in a general form like this:
new Classname(); // Creating an object of class.
The class name followed by parentheses represents the constructor of the class.
=== Linking Object and Reference ===
We will link the object and reference created above like this:
Classname object_reference_variable = new Classname(); // This is a syntax for creating an object of class in java.
Assigns the new Student to the reference variable s.
Student s = new Student();
where,
Student➝ Name of the class.
s➝ Object reference variable which stores the address of the object in the stack memory.
new ➝ keyword that stores the object in the heap memory.
Student() ➝ Constructor of the class.
= ➝ The equal sign (=) is an assignment operator. It simply says to take the object created by a new keyword and assign it to the object reference variable.
When an object of class is created, the memory is allocated in the heap area to store instance variables.
After creation of an object, JVM produces a unique reference number (address) for that object. This unique reference number is called hash code number.
This reference number is unique for all objects, except string objects.
The address of the object is stored in the object reference variable in the stack memory.
We can know the hash code number or reference number (address) of an object by using hashCode() method of object class.
The following code can be used to know the hash code number.
=== Key points ===
Remember that creating an object in java means allocating memory for storing data.
You can also create an object of the class in two steps like this:
Step 1: Student s; // Declaration of reference to the object.
Step 2: s= new College(); // Creating an object.
#JavaOOPsConceptsTutorials
#LearningandTeachingCoding
In questa pagina del sito puoi guardare il video online Object in Java || How to create an object in java? || Java OOPs Concepts Tutorials della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Learning and Teaching Coding 06 aprile 2021, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 1,394 volte e gli è piaciuto 41 spettatori. Buona visione!