0

So I have a class called Test:

public class Test{
    protected String name = "boy";
    protected String mainAttack = "one";
    protected String secAttack = "two";
    protected String mainType"three";
    protected String typeSpeak = "no spoken word in super class";

//Somehow put all the class variables in an Array of some sort
    String[] allStrings = ??(all class' strings);
//(and if you feel challenged, put in ArrayList without type declared.
//So I could put in, not only Strings, but also ints etc.)

    public void Tester(){
    //Somehow loop through array(list) and print values (for-loop?)
    }
}

As you can see, I want to put all the class variables in an Array or ArrayList (or something similar) automatically. And next I want to be able to loop through the array and print/get the values. Preferably using an enhanced-for loop.

4
  • Use getters and setters. Commented Dec 12, 2013 at 13:44
  • Why don't you just use an array to start with? why do you need the separate variables? Commented Dec 12, 2013 at 13:44
  • This is a terrible idea, don't ever design your classes this way. If you're doing it just to test reflection, fine. Reflection being the key word. Commented Dec 12, 2013 at 13:44
  • It's a small portion of my project. Just a simple example to see what my intentions are. This is not my actual code. Commented Dec 12, 2013 at 13:47

5 Answers 5

1

As other said, don't do this. But this is how:

Class<?> cl = this.getClass();
List<Object> allObjects = new ArrayList<Object>();
for (java.lang.reflect.Field f: cl.getDeclaredFields())
{
    f.setAccessible(true);
    try
    {
        Object o = f.get(this);
        allObjects.add(o);
    }
    catch (Exception e)
    {
        ...
    }
}
for (Object o: allObjects)
    System.out.println(o);
Sign up to request clarification or add additional context in comments.

Comments

0

If you really really need do this you need to use Reflection.

However a much better approach would be to store the values in a Map (probably a HashMap) and then you can query/set/etc them from that easily.

Comments

0

You can use Map or Hashmap to store variables and its values instead of Array or Arraylist

HashMap is an object that stores both “key/value” as a pairs. In this article, we show you how to create a HashMap instance and iterates the HashMap data.

Comments

0

Why not use a HashMap for the values and iterate through that?

Iterate through a HashMap

1 Comment

I understand, and you are correct. But that doesn't apply to my problem right now...
0

Do this.

String threeEleves = "sky";
String sevenDwarves = "stone";
String nineMortal = "die";
String oneRing[] = new String[]  // <<< This
{
  threeElves,
  sevenDwarves,
  nineMortal
}

or do this

// in some class.
public void process(final String... varArgs)
{
    for (String current : varArgs)
    {
    }
}

String one = "noodles";
String two = "get";
String three = "in";
String four = "my";
String five = "belly";

process (one, two, three, four, five);

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.