7

I have been working with Java and C# for a while now and I've been asking myself this many times but haven't ever found the answer I was looking for.

When I have to call an object method (this means it's not static), I have to use the call through instance of class, for example:

MyClass myInstance = new MyClass();
myInstance.nonStaticMethod();

I see this kind of code everywhere, so I was thinking if one-line call (example below) is behaving differently performance wise or it's just the sake of standards?

This is what I meant with one-line call:

new MyClass().nonStaticMethod();
5
  • You could compile that java code and check the byte code to see the differences. Performance-wise you won't see any difference. Commented Nov 1, 2013 at 22:00
  • You could ask yourself when you would want to make one-line method calls like that, and then ask yourself if you shouldn't make these methods static. This is exactly what you need that static keyword for: helper methods that don't really need an object. An example is Math.abs(int) which returns the absolute value of the input. Commented Nov 1, 2013 at 22:02
  • @GrantWinney Can you dispose of objects in C#, you can't in Java. Commented Nov 1, 2013 at 22:02
  • 1
    @PeterLawrey: The Closeable/AutoCloseable interfaces in Java correspond, more or less, to .NET's IDisposable. Commented Nov 1, 2013 at 22:04
  • @GrantWinney And that frees up the memory for that object at that point and the GC doesn't have to do anything or does it just close the object? This may be similar to the try-with-resource block in Java 7. Commented Nov 1, 2013 at 22:09

4 Answers 4

7

The performance would probably be the same.

However, having calls such as new MyClass().nonStaticMethod(); usually reeks of a code smell - what state was encapsulated by the object that you only needed to invoke a method on it? (i.e. Why was that not a static method?)

EDIT: I do not intend to say it is always bad - in some cases, such idioms are encouraged (such as in the case of fluent builder objects) - but you will notice that in these cases, the resulting object is still significant in some way.

Sign up to request clarification or add additional context in comments.

2 Comments

If you always do this when calling that method, then the method probably should have been static. If you only sometimes do this, then it may be fine. Doing this occasionally isn't a big red flag, although doing it a lot would be.
@Servy: Well, now that I think about it, there are some APIs (the fluent interface APIs in particular) where such idioms can actually be encouraged, as they generally make use of method chaining.
2

I would go with the first way i.e.

MyClass myInstance = new MyClass();
myInstance.nonStaticMethod();

The problem with the second one i.e. new MyClass().nonStaticMethod(); is in case you want to call another method from same object you don't have any choice The only thing you can do is new MyClass().nonStaticMethod1(); which actually creates a new object everytime. IMHO I don't think any one of them would performance better than the other. IN lack of performance gain I would definitely choose the one which is more clear and understandable hence my choice.

Comments

2

If you look at the byte code generated you can prove there is absolutely no different to the performance or anything else. Except you are using a local variable which should be discarded.

Unless you have measured that you have a performance problem, you should assume you don't and guessing where a performance problem might be is just that, no matter how much experience you have performance optimizing Java applications.

When faced with a question like this, you should first consider what is the simplest and clearest, because the optimizer looks for standard patterns and if you write confusing code, you will not only confuse yourself and others but the optimizer as well and it is more likely to be slower as a result.

Comments

1

Assuming you never need to access the object's instance again, there is no difference. Do whichever you prefer.

Of course if you want to do anything else with that object later on you'll need it in a variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.