DEV Community

Mitz
Mitz

Posted on

var var = var().var.var()

var was introduced in Java10. It's interesting!

jshell> Var var(){return new Var();}
|  created method var(), however, it cannot be referenced until class Var is declared

jshell> class Var{Var1 var = new Var1();}
|  created class Var, however, it cannot be referenced until class Var1 is declared

jshell> class Var1{Var var(){return new Var();}}
|  created class Var1

jshell> var var = var().var.var();
var ==> Var@58651fd0
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
alainvanhout profile image
Alain Van Hout • Edited

Your mileage may vary ...

Collapse
 
bufferings profile image
Mitz

Indeed. I would reject it in no time if anyone send a pull request like this :)

Collapse
 
alainvanhout profile image
Alain Van Hout

Yes, there’s a big difference between brevity and maintainability, which is something quite a lot of people apperently do not realize.

Thread Thread
 
bufferings profile image
Mitz

You're right. I think this article is a good starting point how to use/not to use var :)

Style Guidelines for Local Variable Type Inference in Java