While learning Java’s basics, I remember coming across a particular syntax for passing arguments to a class constructor. I found this syntax extra read-able, but I’m sadly not able to find it anymore. It looked somewhat like the following:
// Creating an instance of the Employee class (has property name, salary, etc)
Employee fred = new Employee({
name: "Fred",
salary: 5000
job: Jobs.PROGRAMMER
});
As you probably can see, it becomes very clear what each argument to the constructor means, which eliminates the need to look at documentation – just to understand simple code.
Am I mixing languages up or does a syntax somewhat like this exists? An eventual link to the manual would be appreciated.
This is not Java syntax (and never was).
What you can do is to achieve something similar is to use Anonymous classes with initializers like this
There is an explanation of what is going on here: http://blog.schauderhaft.de/2012/08/19/named-parameters-in-java-another-alternative/