-2

There are examples of calling such methods on any class.

I.e.:

SampleClass sc=new SampleClass();
sc.someMethod();

Or is it better to use

new SampleClass().someMethod();

Please, explain in detail.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Chirag.T
  • 746
  • 1
  • 6
  • 18
  • Unclear what you're asking. Obviously the first is better if you actually need the variable for the object. Also, not sure why this is tagged with Android – OneCricketeer Oct 06 '16 at 05:55
  • tagged with android b'cause am i want to know any different while or any other solution when using in android – Chirag.T Oct 06 '16 at 06:02
  • Java is Java. You have no Android specific code here. Yes, you'll see AsyncTasks do what you are describing, but you didn't specify that – OneCricketeer Oct 06 '16 at 06:03

4 Answers4

3

Both options are as good, but first one is better...

If you use

SampleClass sc=new SampleClass();
sc.someMethod();

You can call other methods of this class using same object of class.

If you use

new SampleClass().someMethod();

You require another object to call other method of this class.


Other example is

loop { // Here loop can be any type, for/while/do-while
    new SampleClass().someMethod();
}

this will create objects of same class as many times your loop execute. But if you go with first option

SampleClass sc=new SampleClass();
loop { // Here loop can be any type, for/while/do-while
    sc.someMethod();
}

This will not cause to create many objects to call method.


But Yes, if your need is to call only one method and that is not into loop, you can go with new SampleClass().someMethod();

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • is occur any memory issue when always use with `new` keyword and that is create always new instance..? @Pankaj – Chirag.T Oct 06 '16 at 05:59
  • `loop { // Here loop can be any type, for/while/do-while new SampleClass().someMethod(); }` means this occur `new` instance every time so this take memory load for every instance..? – Chirag.T Oct 06 '16 at 06:05
  • 1
    @Chirag.T Yes correct. And you should always take care about creating a new object. You should always think of the ways to avoid creating objects as less as possible.\ – Pankaj Kumar Oct 06 '16 at 06:09
2

Other than the obvious that sc remains an available variable in the first option that you can later call instance methods on, both are perfectly valid without additional context.

If you have a class method, (in Java terms, a static method), on the other hand, the object doesn't need constructed

SampleClass.someMethod();
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

It depends. If you want to use anyother methods of SampleClass later, you might keep that variable (sc) alive in the instance. Otherwise,

new SampleClass().method();

is suffice.

O_o
  • 1,103
  • 11
  • 36
0

The first option allows you to access the object in the future again. You have a reference to it, so you will be able to invoke other methods of this object.

SampleClass sc = new SampleClass();
sc.someMethod();

and later:

sc.someOtherMethod();
Wojtek
  • 1,288
  • 11
  • 16