2

I have the following class:

 public final class FileNames
{

    private FileNames()
    {}

    public static final String A                     = "a.csv";
    public static final String B                     = "b.csv";
    public static final String C                     = "123.csv";
    ...
}

Now I want to get all String values from the above, somewhere in a different class.Output can be as a String array or ArrayList Or any other Collection. I am using jdk 1.7. No clue how to proceed with this as the list of Strings can be increased,i.e. may add "D","E" etc..

Koustav Ray
  • 1,112
  • 13
  • 26
  • 2
    Enumerations are no possibility for you? - Every enumeration comes with a *values()* method which lists all defined values. – Alexander Apr 06 '16 at 15:18
  • This seems like a design issue. If you know these strings to be constant, is there a *better* place you can put them, like a file? – Makoto Apr 06 '16 at 15:22
  • @Alexander enums are also ok..but when I try to use switch case on the enum values it asks for string constants and I am not ok with using the string values as the same as the enum name..I mean A need not have "A" as a value – Koustav Ray Apr 07 '16 at 07:52
  • @Makoto agreed..that's a better solution,but unfortunately I need it to be in java classes or enums..not allowed to store any properties file for the same.. – Koustav Ray Apr 07 '16 at 07:54

2 Answers2

4

Reflection is your friend :)

public class Sample {

    public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
        Class<FileNames> clazz = FileNames.class;
        Field[] arr = clazz.getFields(); // Get all public fields of your class
        for (Field f : arr) {
            if (f.getType().equals(String.class)) { // check if field is a String
                String s = (String)f.get(null); // get value of each field
                // add s to a List
                System.out.println(s);
            }

        }
    }

}

 final class FileNames {

    private FileNames() {
    }

    public static final String A = "a.csv";
    public static final String B = "b.csv";
    public static final String C = "123.csv";

}

O/P :

a.csv
b.csv
123.csv
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • This definitely *works*, but...ew? Maybe the OP can clarify if there's a better place that they can place these strings. – Makoto Apr 06 '16 at 15:25
  • @Makoto - I think you are right. This could be a XY problem – TheLostMind Apr 06 '16 at 15:27
  • 1
    Works like a charm! Thanks.. And Yes I did want the Strings in an array instead of a Sysout..But that I can manage pretty well :P.. Thanks again @TheLostMind – Koustav Ray Apr 07 '16 at 08:04
-1

You add this function:

public static ArrayList<String> getFilenames() {
   ArrayList<String> filenames = new ArrayList<String>;

   filenames.add(A);
   filenames.add(B);
   filenames.add(C);

   return filenames;
}

Then you can use this function somewhere like:

ArrayList<String> filenames = new ArrayList<String>;
filenames = Filenames.getFilenames();
String filenameA = filenames.get(0); // to retrieve the first element (A)
String filenameB = filenames.get(1); // to retrieve the second element (B)
// and so on...

If you want to add more and convert it in an auto way you can check this: Java method with unlimited arguments

I hope I could help you.

Community
  • 1
  • 1
Wolfman
  • 49
  • 5