Is it ok if the program is written like this :
class ReduceCode
{
void display()
{
System.out.print("Hello");
}
public static void main(String[] X)
{
new ReduceCode().display();
}
}
instead of
class ReduceCode
{
void display()
{
System.out.print("Hello");
}
public static void main(String[] X)
{
ReduceCode rc = new ReduceCode();
rc.display();
}
}
I understand the reason behind having to declare a reference to an object so that if there are any instance variables involved, in the future the value from those variables can be accessed using that reference, but for methods like display()
which only prints something, the reference can be ignored.
Apart from this, does instantiation without reference have any disadvantage? I couldn't find any documentation on the official website. Is this technique used at industry level at all?