mins mins - 1 year ago 69
Java Question

Don't understand @ConstructorProperties



Documentation says "An annotation on a constructor that shows how the parameters of that constructor correspond to the constructed object's getter methods". And it gives an example which is ambiguous because variable names are identical to parameters.

I really don't get why
@ConstructorProperties({"x", "y"})
refers to getters
. Case of x and y are not consistent with annotation.

So to clarify what should be the annotation for the constructor in this code:

public class Point {
public Point(int a, int b) {
this.c = a;
this.d = b;

public int getCc() {
return c;

public int getDd() {
return d;

private final int c, d;


(I edited the code because from the answer, I understand the annotation expects the code to follow common case convention for getters, e.g.
getter must be
. But I keep on purpose, for disambiguation, the difference between getter name and actual variable returned)

Second question...


What does this annotation means, for JButton(String text)?

It seems provided for use by tools, but just want to understand.

Answer Source

It complies with common naming conventions for getter and setter, the example using single letters isn't the best though. Basically you capitalize the property's name and prepend get or set (or is for booleans).

E.g. someValue becomes getSomeValue

Keep in mind that method parameter names aren't visible at runtime. It's the order of parameters that counts. The names of properties and variables used in the constructor don't matter. The following would still refer to a method named getSomeValue().

public Point(int a) {
    this.c = a;

The use case for this annotation seems to be (de-)serialization of immutable objects. The framework gets all values using the objects getter and can serializes the object. When the object needs to be deserialized, the framework needs to create a new instance. Because the object is immutable, it doesn't have any setters. The constructor is the only way to set those values for the framework and the annotation is used to tell it how to recreate the serialized object.