M
Miguel Farah
I was recently sent some Java code that I find VERY odd. Among several
other things, all the ValueObject classes are like this example:
----8<--------8<--------8<--------8<--------8<--------8<--------8<----
package this.cant.be.right;
import java.io.Serializable;
public class WhatTheHell implements Serializable {
private String text;
public static WhatTheHell
HELLO = new WhatTheHell("HELLO"),
WORLD = new WhatTheHell("WORLD"),
DAZED = new WhatTheHell("DAZED"),
CONFUSED = new WhatTheHell("CONFUSED"),
DAMMIT = new WhatTheHell("DAMMIT");
protected WhatTheHell(String text) {
super();
this.text = text;
}
public String getText() {
return text;
}
}
----8<--------8<--------8<--------8<--------8<--------8<--------8<----
There are some obvious flaws: the setText() method is missing and the
constructor uses super() even though it doesn't extend anything
(besides
Object, that is).
However, what I REALLY find odd is the static atributes HELLO, WORLD,
etcetera, as instances of the class itself. The guy that did this
states
this lets him restrict the the constants that might be used to
instantiate objects of this class.
*I* find this very odd, and have serious doubts about it. Since I'm not
yet expert at Java, I could be mistaken, and this is why I ask you guys
to tell me wether I'm right or wrong. Have you seen this before? Is it
considered a good practice? Or not?
Thanks in advance.
other things, all the ValueObject classes are like this example:
----8<--------8<--------8<--------8<--------8<--------8<--------8<----
package this.cant.be.right;
import java.io.Serializable;
public class WhatTheHell implements Serializable {
private String text;
public static WhatTheHell
HELLO = new WhatTheHell("HELLO"),
WORLD = new WhatTheHell("WORLD"),
DAZED = new WhatTheHell("DAZED"),
CONFUSED = new WhatTheHell("CONFUSED"),
DAMMIT = new WhatTheHell("DAMMIT");
protected WhatTheHell(String text) {
super();
this.text = text;
}
public String getText() {
return text;
}
}
----8<--------8<--------8<--------8<--------8<--------8<--------8<----
There are some obvious flaws: the setText() method is missing and the
constructor uses super() even though it doesn't extend anything
(besides
Object, that is).
However, what I REALLY find odd is the static atributes HELLO, WORLD,
etcetera, as instances of the class itself. The guy that did this
states
this lets him restrict the the constants that might be used to
instantiate objects of this class.
*I* find this very odd, and have serious doubts about it. Since I'm not
yet expert at Java, I could be mistaken, and this is why I ask you guys
to tell me wether I'm right or wrong. Have you seen this before? Is it
considered a good practice? Or not?
Thanks in advance.