With regards to object-oriented Java constructor syntax and design, specifically parameterized or specific constructors, it is the accepted process, as describe in books, to build a parameterized constructor in this form:

public Car (int x, int y, int g) {
   xdistance = x;
   ydistance = y;
   gas = g;


For shorthand and simplification, why can we not build the constructor in this form?

public Car (int xdistance, int ydistance, int gas) {

This is a cleaner approach, with less code, yet I do not see this syntax adopted in tutorials.

  • 3
    sure its possible to have any syntax you dream up but language design just doesn't work that way blogs.msdn.microsoft.com/ericgu/2004/01/12/minus-100-points – jk. Mar 11 '19 at 13:09
  • Do you mean, without the instance variables initialisation? – Laiv Mar 11 '19 at 13:16
  • 4
    Are you asking whether that code is possible in Java (it is not), whether a language could use such syntax (sure), or why Java was designed the way it is (we didn't do it and can just take a guess)? Note also that Java was never a terribly innovative language, and mostly just combined Smalltalk semantics with C++ syntax. Java's constructor syntax is much simpler than the one used in C++. – amon Mar 11 '19 at 13:23
  • 1
    "when we set the parameters in the parenthesis this tells JVM that we have definitions for those parameters in the body" I'm not quite sure what you're trying to say here, but I suspect there might be a misunderstanding. – user214290 Mar 11 '19 at 13:37
  • 1
    Take a look at Scala, it provides a syntax very similar to what you are looking for. – Giorgio Mar 11 '19 at 16:37

Java simply isn't defined that way. The parameter names in a constructor can be whatever you like, and they are entirely unrelated to the field names in the class.

You could propose a change to the Java language that allowed for that, but it is unlikely to be accepted in it's current form, because your proposed syntax will change the meaning of currently valid Java programs. Your example is valid Java, it takes three ints and does nothing with them.

  • IMO, this would only break existing code that is already broken. A constructor that completely ignores it's arguments is, well, broken, Also, perhaps, the new syntax could end with a semicolon instead of empty brackets (like an interface definition). That said, I agree that this new proposed syntax is unlikely to ever be accepted. (Too bad, I like the simplicity a lot) – user949300 Mar 11 '19 at 17:54
  • @user949300 it's not just this clear cut example. What about constructor parameters that are used, but are currently named the same as a field? or of a base classes' field? of a base classes' private field? etc. – Caleth Mar 11 '19 at 22:02

As others already said, Java isn't defined that way. In Java, we tend to write down everything quite explicitly, and I personally like it that way, as it generally improves code reliability.

Imagine a small typo:

public Car (int xDistance, int yDistance, int gas) {

If I'd read that, I'd expect that to initialize the three fields. But how will the compiler interpret that? xDistance and yDistance will now simply be normal constructor arguments, not initializing any field. Depending on your IDE / compiler settings, you might at best get an "unused argument" warning. So, I strongly prefer not having such a fragile automatic initialization.

And for boilerplate code like such a constructor, e.g. Eclipse has the "Source / Generate constructor using fields..." wizard. A few clicks, and you have the constructor.

  • @Ralk thanks for another point-of-view. – Minimalist Mar 11 '19 at 20:33
  • Relying on an IDE to compensate for overly complex syntax / boilerplate is just papering over the problem. – user949300 Mar 11 '19 at 20:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.