Skip to content

Instantly share code, notes, and snippets.

@joschuck
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save joschuck/1d8b290945ac4ab3e27c to your computer and use it in GitHub Desktop.

Select an option

Save joschuck/1d8b290945ac4ab3e27c to your computer and use it in GitHub Desktop.
Java Reflection API - array from final static Strings
public class Name {
private final static String = "Alice";
private final static String = "Eve";
private final static String = "Bob";
}
// somewhere else in the program
Name name = new Name();
Field[] fields = Name.class.getDeclaredFields();
List<String> names = new ArrayList<String>();
for (Field field : fields) {
if (java.lang.reflect.Modifier.isStatic(field.getModifiers()) && field.getType() == String.class) {
field.setAccessible(true);
names.add((String) field.get(edgeType));
}
}
// names ["Alice", "Eve", "Bob"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment