Scientech Easy How to create object in Java with example & programs | Scientech Easy

Saturday, July 28, 2018

How to create object in Java with example & programs

Welcome to the online Java tutorial. In this chapter, we will learn How to create an object in Java with some simple example and programs which will be helpful to understand the concept of an object. 

How to create an object of a class in Java

There are several ways to create an object of the class in Java. They are:
a. Using new keyword
b. Using Class.forName
c. Using Clone.
d. Using Object Deserialization.
e. Using ClassLoader.
In this chapter, we will learn using the new keyword to create the object of the class and another way we will learn one by one in the next tutorial.

By using the new keyword
It is the most common and straightforward way to create an object in Java programming. It is also very simple and easily understandable to create an object of the class in the program. 
What is the new?
new is a special keyword which is used to create an object of the class. It allocates the memory to store the objects whose type is specified by a constructor and then calls the constructor to initialize the object which is stored in the heap(a region of memory for storing object).  When the constructor ends, new return memory addresses to the object so that it can be accessed from anywhere in the application. See below image.
How to create object in Java with example, How many ways to create object of the class in Java?
How To create object of the class

The general form of a statement is
ClassName object_Reference_variable_name=new ClassName();
Now consider the example to an object/instance of a class College.
      College cg=new College();
where 
 College Name of the class.
   cg       Object reference variable which stores the address of the object in the stack memory.
new      keyword that stores the object in the heap memory.
College() Constructor of the class.
   =         The equal sign(=) is an assignment operator. It simply says to take the object created by new keyword and assign it to the object reference variable.
By using this method, we can call the no-argument constructor as well as parameterized constructor.
For example:
    Student st=new Student(); // It will call default constructor.
    School sc=new School("DEEP"); // It will call Parameterized constructor.
Program Source Code: Let's take a very simple program step by step. 

    package scientech; // Step 1: Declare the class with the name. public class HelloJava { // Step 2: Declare the constructor with the class name. This constructor is a default. HelloJava(){ // It will print the message on the console. System.out.println("Hello Java"); } // Step 3: Declare the method and Print any message which you want. void display(){ System.out.println("Welcome to online Java tutorial point. "); } // Step 4: Declare the main method. This method is static. public static void main(String[] args) { // Step 5: Create the object of the class with the reference variable 'obj'. HelloJava obj=new HelloJava(); // It will call default constructor as object created. // Step 6: Now call the method to print the output on console using object reference variable 'obj'. obj.display(); } }
    Output: Hello Java Welcome to online Java tutorial point.
You follow all the above steps to make your program easy and keep all steps in mind. 

What is void?
The void is a keyword which indicates that this method does not provide or return any data back to the class of an object.

Now let's take one more example for practice.
Program source code 2: In this program, we will calculate the sum of two numbers. you follow all the above steps.

    package scientecheasy; public class Sum { // Declare instance variable. int a; int b; // Declare the default constructor and initialize the value of variables. You can also initialize directly to the variables. This is another way to initialize the value of variables. Sum(){ a=50; b=20; } // Declare the method. void display(){ int sum=a+b; System.out.println("Sum of two numbers: " +sum ); } public static void main(String[] args) { // Create the object of the class and call the method using reference variables to print output on the console. Sum sm=new Sum(); sm.display(); } }
    Output: 70
Final words:
I hope that this article will help to understand the topic How many ways to create an object in Java for example and programs. I hope that important topic covered in this chapter and We always try to understand you in an easy way. In the next tutorial, we will learn different ways to initialize the objects.


Next ➝ Object Declaration and Initialization