3

I am looking for a way to have a generic class with dynamic numbers of properties.

Example: (Class User)

User user1 = new User(1,"john","115 street","male",60,150, 15.125);
User user2 = new User(2,"john2","116 street","male",60,150, 15.125,"New york");

etc..

Can this be done in Java or could you recommend me any possible alternatives (if not)?

1
  • In strong languages such as Java, you can't dynamic create types because all types specified in compile-time. Your request solved in dynamic languages such as Python or Ruby.
    – Sam
    Feb 19, 2012 at 13:16

5 Answers 5

7

Unfortunately, Java doesn't support dynamic properties.

You can hack that with:

  1. Vararg methods. Use the following signature method(Object... objects). In that case you will have array of objects of your parameters passed to method - just process them as needed.

  2. Map structure. I think its better solution, you have map with parameters inside your class. Where is key - name of your parameter and value its actual value.

    class User {    
    
    Map<String, Object> properties;
    
          Object getParameter(String key){
            return properties.get(key);
          }
          void addParameter(String key, Object value){
            properties.put(key, value);
          }
    
    }
    

Use method addParameter to add new parameter to your object.

Use method getParameter to extract value that needed (e.g getParameter("name");)

2
  • You should not replace "normal", static properties (like name, address...) with a map. You loose type safety, editor support, spell check/compiler support (getParameter("nam") is valid), Java Beans support. Especially with the latter all tools that rely on this (like e.g. Hibernate) will fail or need extra work. Use a map only for truly dynamic parameters. Feb 19, 2012 at 19:50
  • Sure. Even if attribute is optional you should not use Map (better to use Builder pattern)
    – mishadoff
    Feb 20, 2012 at 10:25
7

You can create a varargs constructor in Java. In your case it would look like this:

public class User {
  private final long id;
  private final String name;
  private final Object[] parameters;

  public User(long id, String name, Object... parameters) {
    this.id = id;
    this.name = name;
    this.parameters = parameters;
  }
}

Although I think it's not a good pattern, and I would recommend you to come up with a better idea, for example a Map<String, Object> containing the optional parameters, or something like that.

2

In your case(in my opinion) it will be better to use builder pattern. See here. Or see the Bloch's item 2 in Effective Java

2

Yes, it is possible, but probably it is not a good idea.

You can do:

User(Object... args) {
    Object someObject = args[0]; // Check for length!
}

This is called varargs in Java lingo. But you get all your arguments as simple Objects within an Object[] and have to find a method to interpret them. The caller does not know what parameter to put at what position. If you want to allow this you typically won't use a language with Java's type and binding system.

The prime alternative -"the Java way of doing it"- is to overload the method; you can have more than one method with a given name and different arguments in a type. Example:

User(long id, String name) { ... }
User(long id, String name, String address) { ... }
// and so on

This can be cumbersome, so the IDEs have tools to generate these for you (at least for constructors).

1

This is a candidate for the Builder pattern

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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