object
Definition 1
- Instance of a class is known as an object
- Instance is a way of allocating the sufficient amount of memory for data members.
Definition 2
- Group of items exists in single entity is known as an object.
- We can say single entity means an object variable which holds group of values.
Definition 3
- Real world entities are called as objects.
Definition 4
- Logical run time entities are called as objects.
Bulletin points
- An object can exists physically in this world but class does not exist.
- An object does not exist without class but class can exist without object
- Read again above statement for better understanding.
- Simply i can say object will come from class.
- Every object having state, behavior and identity of an object.
- we can create object by using new keyword.
Syntax
Classname objectname = new constructor();
Example:
Student s1 = new Student();
- Classname(Student) represents the name of the class
- objectname(s1) represents any java valid variable name.
- by using new(new) operator we will create an object for class.
- If constructor(Student()) will exists in class(Student) then at the time of object creation it will be called automatically
Note
- We will learn clearly about constructor in upcoming chapter.
Why should we create an object?
- Once if you define any variable or data in java program then immediately memory will be not allocated for that data.
- If we create an object for that class then memory will be assign to the data.
--------------------------------------------------------------------------
Program : Sample class and object program
Program name : Demo1.java
Output :
Id is : 1
Name is : Mouli
class Demo1
{
int id = 1;
String name = "Mouli";
public static void main(String args[])
{
Demo1 demo1 = new Demo1();
System.out.println("Id is : "+demo1.id);
System.out.println("Name is : "+demo1.name);
}
}
Compile : javac Demo1.java
Run : java Demo1
Output :
Id is : 1
Name is : Mouli
--------------------------------------------------------------------------
Thanks for your time.
Thanks for your time.
Nireekshan