When to define object and when to use "new" only
Suppose we have a class, "SampleClass" containing a, say, public void method "f()". If I wanted to call this method, I could use a SampleClass sc = new SampleClass(); then call f: sc.f(); or I could just say: new SampleClass().f();
second one seems better when I just want to use the f() and nothing else. It seems better for garbage collection (?)
On the other hand say some time later I come back and I need to change that piece of code. (say I need another call to the f() method somewhere else in the same block of code) then this would have been better to just define the object in the first place. This means the second approach makes my code less maintainable.
Now the question is which approach would be better?