Is it good design to have a constructor with parameters being not within the class of the constructor?
So you see I have two properties within my constructor only for internal calculation to represent the property newDate. Is that totally okay or should I do that in a method which returns a new Instance of User instead of a constructor?:
There's nothing wrong with using a constructor that way. However, a factory method has the advantage of being able to create subclasses of the return type, and it isn't even required to return an instance at all.
I generally use factory methods for my "value" classes, immutable classes that model some sort of value (examples from the API are Integer, String, etc), so I have the option to add value caching to my class later on, even if I usually don't. With most classes though, I usually just make a public constructor.
If you can foresee that you want to provide clients with subclasses of your User class later on, you should use factory methods. Otherwise I would just go with the constructor.
Wait what others have to say, they may have much more insight in this matter than I do.
Joined: Oct 13, 2005
I presume when you wrote an int followed by a dot . you were trying to see whether the rest of us are awake.
Joined: Jun 28, 2008
The point of the question is: Is it okay to design something like that? Would it be really better to use something like CDI (Context and Dependency Injection) with Factory injection?