steps to create an object of a class in java please write step by step and please answer me
Answers
Answered by
18
Definition :
- Objects are the basic run time entity or in other words object is a instance of a class. All these objects have a state and behavior.If we consider the real-world we can find many objects around us, Cars, Dogs, Humans etc. If we consider a dog then its state is name, breed, color, and the behavior is barking, wagging, running.Software objects also have a state and behavior.
Creating an Object :
In java the new keyword is used to create new objects.There are three steps when creating an object from a class.
Declaration : A variable declaration with a variable name with an object type.
Instantiation : The 'new' keyword is used to create the object.
Initialization : The 'new' keyword is followed by a call o a constructor. This call initializes the new object.
Syntax for creating Object :
class_name object_name = new class_name();
Example :
class demo
{
int i=10;
void display()
{
System.out.println("www.java-answers.blogspot.com");
}
public static void main(String args[])
{
demo x = new demo();
x.display();
System.out.println("value of i = " +i);
}
}
Output :
www.java-answers.blogspot.com
value of i = 10
Accessing Methods Using Object :
Instance variables and methods are accessed via created objects.you can learn about Methods and how to create Methods from what are methods in java tutorial. To access a Method from the above example should be as follows:
/* First create an object */
demo x = new demo();
/* Now you can call a class method as follows */
x.display();
The above example program creates a object with name x using 'new' keyword and after creating the object we can access the method and variable of class using this objects as shown in the above program.This is how objects are created in java.
HOPE IT HELPFUL DEAR AND MARK ME BRAINLIST
- Objects are the basic run time entity or in other words object is a instance of a class. All these objects have a state and behavior.If we consider the real-world we can find many objects around us, Cars, Dogs, Humans etc. If we consider a dog then its state is name, breed, color, and the behavior is barking, wagging, running.Software objects also have a state and behavior.
Creating an Object :
In java the new keyword is used to create new objects.There are three steps when creating an object from a class.
Declaration : A variable declaration with a variable name with an object type.
Instantiation : The 'new' keyword is used to create the object.
Initialization : The 'new' keyword is followed by a call o a constructor. This call initializes the new object.
Syntax for creating Object :
class_name object_name = new class_name();
Example :
class demo
{
int i=10;
void display()
{
System.out.println("www.java-answers.blogspot.com");
}
public static void main(String args[])
{
demo x = new demo();
x.display();
System.out.println("value of i = " +i);
}
}
Output :
www.java-answers.blogspot.com
value of i = 10
Accessing Methods Using Object :
Instance variables and methods are accessed via created objects.you can learn about Methods and how to create Methods from what are methods in java tutorial. To access a Method from the above example should be as follows:
/* First create an object */
demo x = new demo();
/* Now you can call a class method as follows */
x.display();
The above example program creates a object with name x using 'new' keyword and after creating the object we can access the method and variable of class using this objects as shown in the above program.This is how objects are created in java.
HOPE IT HELPFUL DEAR AND MARK ME BRAINLIST
Shivesh08:
jatin i want to write it step by step
Answered by
5
First you must understand the concept of objects and classes in java.
In java, objects are an instance of their respective class and a class is a blueprint of an object. Now, to create an object of a class, keep in mind that the object of an class can only be create inside a main() function whether that function is in that class itself or it is inside another class which is located within the same package.
So, to create an object of a class, write the statement following the given syntax inside a valid main() statement as mentioned above.
class_name obj_name =new class_name(parameter1,parameter2,…)
Similar questions