Is it better to call a method on a variable or chain it to the constructor?
问题 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. 回答1: 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