2

I have a user defined class like the following,

package com.hexgen.tools;

public class UserDefinedParams {
    private String dataType="";
    private String isArray="";
    public String getDataType() {
        return dataType;
    }
    public void setDataType(String dataType) {
        this.dataType = dataType;
    }
    public String getIsArray() {
        return isArray;
    }
    public void setIsArray(String isArray) {
        this.isArray = isArray;
    }

}

dataType and isArray the values for this if dataType -> this may have userdefined pojo class or java primitive type and if isArray ->this will have Y or N. based on this how to create something like:

if dataType someUserDefinedPOJO and isArray Y

-> someUserDefinedPOJO[] obj = new someUserDefinedPOJO();

vise versa for java primitive types too.

is it possible through reflection in java?

How to do this?

Best Regards

4
  • What do you want to achieve by this ? Do you want to add the dynamic variables and the corresponding code to your source code ? Commented Apr 24, 2013 at 9:24
  • I'd suggest you have a look at reflection in Java. However, if this is an important functionality in your program, you will be much easier off with a weak typed programming language. Commented Apr 24, 2013 at 9:24
  • I'm not sure I'm following, but note that Object can store pretty much anything. Regardless, you'd be much better off redesigning your program such that what you're trying to do is no longer required. Commented Apr 24, 2013 at 9:32
  • i have edited my question, hope this would make sense now. Commented Apr 24, 2013 at 9:35

4 Answers 4

4

just use a HashMap<String,Object> , you can define all your variables in there, for example:

HashMap<String,Object> map=new HashMap<String,Object>();
map.put("myVarName",new Object());

System.out.printlb(map.get("myVarName"));
Sign up to request clarification or add additional context in comments.

2 Comments

this is when we know what is going to come, if so what is the need for me to create instance dynamically and create it at source level itself
its the best imitation of dynamic variables, you can put any name that comes from any place, its a string
1

There are no dynamic variables in Java. Java variables have to be declared in the source code.

Depending on what you are trying to achieve, you should use an array, a List or a Map; e.g. See here.

int n[] = new int[3];
for (int i = 0; i < 3; i++) {
    n[i] = 5;
}

List<Integer> n = new ArrayList<Integer>();
for (int i = 1; i < 4; i++) {
    n.add(5);
}

Map<String, Integer> n = new HashMap<String, Integer>();
for (int i = 1; i < 4; i++) {
    n.put("n" + i, 5);
}

Comments

1

I think you should look into java reflection. But this has already posted on SO.

Read link 1

Read link 2

Comments

1

Another possible route is to go with generics:

public class CustomVariable<E> {
    private E var;

    public CustomVariable<E>(E value){
        var = value;
    }
}

But I wouldn't know about arrays though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.