How To Create An Object In Java

How To Create an Object In Java

There are different ways to create an object in a java class. Java classes provide the framework for creating objects, objects are created from java classes. Being an object-oriented programming language, concepts are described in terms of objects. 

The two popular ways to create objects in java are:

  • Using the new keyword:

The use of the reserved keyword “new” is the most popular and commonly used way to create an object in java. We use this method to call the constructor we want. The constructor can either be the no argument constructor or the parameterised one.

Example: 

import java.math.BigDecimal;

public class Car {   
private String model;
   private String Colour;
   private BigDecimal price;  
 public Car() { 
  }   
public Car(String model, String colour, BigDecimal price) {      
 this.model = model;     
  Colour = colour;  
     this.price = price; 
  }
   public String getModel() {  
     return model; 
  }
   public void setModel(String model) {   
    this.model = model;  
 }
   public String getColour() {   
    return Colour;  
 }
   public void setColour(String colour) {     
  Colour = colour;   
}
   public BigDecimal getPrice() {  
     return price;   
}
   public void setPrice(BigDecimal price) {   
    this.price = price;  
 }
   @Override   public String toString() {  
     return “Car{” +           
    “model='” + model + ‘\” +        
       “, Colour='” + Colour + ‘\” +        
       “, price=” + price +          
     ‘}’;  
 }
   public static void main(String[] args) {  
     Car tesla = new Car();    
   Car ford = new Car(“ford”, “red”, new BigDecimal(2000));
       System.out.println(tesla);    
   System.out.println(ford);   }
}
Output: Car{model=’null’, Colour=’null’, price=null}Car{model=’ford’, Colour=’red’, price=2000}

In the example above, the tesla car was created with a no argument constructor hence the details were null. The ford car however, was created with the parametarised constructor and the details were updated.

  • Using the new instance

In this case, if we know the name of a class using its default constructor can be used to create an object of the class. To do this, you have to use the new Instance method of the class like this Class.forName

This method of creating an object in Java has been deprecated but it used to be popular and widely used. 

Example: 


public class MyClass {   
public MyClass() {  
 }
   public void someMethod() {      
 System.out.println(“Hello from someMethod!”);  
 }
   public static void main(String[] args) {   
    try {         
  Class<?> myClass = MyClass.class;
           MyClass myObject = (MyClass) myClass.newInstance();
                     myObject.someMethod();    
   }
catch {
(InstantiationException | IllegalAccessException e) {       
    e.printStackTrace();       
}  
 }}
Output: Hello from someMethod!

These are the two common ways to create an object in java, however, the first method using the “new” keyword is the preferred and industry standard. Unless it is an absolute requirement, do not use the newInstance method.

This article was written by Joy Ezinne Chime for Deepthink Advanced Technologies Ltd

Leave a Comment

Your email address will not be published. Required fields are marked *