Vijay
Vijay

Reputation: 8451

How to create object of java class

This code working fine for me.

public class ABC 
{
  public int sayHello()
  {
    System.out.println("Hello Friends...!!!");
    return 1;
  }
}

public class XYZ
{
  public static void main(String[] args){
    // 1 Option
    ABC objABC = new ABC();
    int i = objABC.sayHello();

    // 2 Option
    int j = new ABC().sayHello();
  }
}

Here I have called sayHello() method of Class ABC with two different way. I just want to know which option is optimum and good practice to used.

Upvotes: 1

Views: 238

Answers (8)

Panchotiya Vipul
Panchotiya Vipul

Reputation: 1296

Definitely the first option

1) If you need to invoke the same method again, you won't have to invoke the constructor again. That is, you won't create another instance and save the load time for create new object.

2) If you want to close a stream or set it to null (like it is required for IO related operations), second option will not give you any reference object to do so and every call the constructor.

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68393

Definitely the first option

1) If you need to invoke the same method again, you won't have to invoke the constructor again. That is, you won't create another instance.

2) If you want to close a stream or set it to null (like it is required for IO related operations), second option will not give you any reference object to do so.

Upvotes: 2

Manish Doshi
Manish Doshi

Reputation: 1193

If you just want to execute the method, without having a reference to the object.So it is used in calling only one method.

int j = new ABC().sayHello(); 

But,

ABC objABC = new ABC();
int i = objABC.sayHello(); 

This is better if you would need objABC for further calls to sayHello() in the future.

Upvotes: 1

Prasad Kharkar
Prasad Kharkar

Reputation: 13556

  • With ABC objABC = new ABC(), you will always have a reference to the object. With new ABC() you can't access second time.
  • suppose there are two methods. sayHello() and sayGoodBye(), then using the first method. you just need to call them using objABC.sayHello and objABC.sayGoodBye().
  • But in second case, you are creating an object without reference and calling the method on it. You cannot perform some other operation on the same object without referring to it by some reference variable.

So it is always better to use the first option i.e ABC objABC = new ABC();

Upvotes: 1

Deepak Bala
Deepak Bala

Reputation: 11185

You use the reference of class ABC in both cases to make a call to the method sayHello(). In case #1 the reference can be reused to make other calls and in case #2, the reference to the object that was created is lost, so you cannot make any further calls on that instance.

There is no optimum answer as such. Use #1 when you want to retain the instance and a reference to it.

Upvotes: 1

user902383
user902383

Reputation: 8640

ABC objABC = new ABC(); is much better, you have instance of your object and you can use it whenever you want

if you want to do call like int j = new ABC().sayHello(); i think better to have it static, so you can call it without creating class instance ABC.sayHello()

Upvotes: 1

Luke Taylor
Luke Taylor

Reputation: 9599

It depends. If you just want to execute the method, without having a reference to the object from which the method was called:

int j = new ABC().sayHello(); 

If you would like to keep a reference to that object, and reuse it later, then:

ABC objABC = new ABC();
            int i = objABC.sayHello();

Upvotes: 1

ssindelar
ssindelar

Reputation: 2843

The whole idea of creating an object to just call one method is quite strange, but as always it depends.

Option 1: this is better if you would need objABC for further calls to sayHello() in the future.

Option 2: If you are just interested in one call, than this is ok. But I would consider a static method in this case, to save the object instantiation.

Upvotes: 1

Related Questions