Java: Best Practice for Declaration and Initialization

Viewed 1545

I do consider myself quite experienced in Java but there are still some minor basic things that I'm not really sure of.

I always try to write maintainable, easy readable code and aim for highest efficency. For example I only call a "new"-Operator only when it is really needed.

That is because I don't want to allocate memory unnecessarily. But what about supporting Variables?

Lots of people tend to declare a String just to assign some long ass method call like this:

String helper = Class.method1().method2(param).getter();

I always wonder if that doesn't allocate more memory than needed. The getter returns a new Object already and now even more memory is allocated by referencing it in addition. When I have use this getter more than once a helper string is convenient, but if it is needed only once wouldn't it be better to pass that method directly instead of declaring a new variable? Does this actually allocate memory to the heap?

Object a, b, c, d, e ,f , g, h, i, j ...;

I hope some more experienced Java guys than me can tell me how they handle basic stuff like this. thanks! :)

2 Answers
Related