-2

I have a class with some system constants, some of these constants are for an example image types {jpg, png,jpeg}

In another class I am checking whether the provided extension is one of supported extensions in the systemConstants class or not.

I want to do such check with a loop, I do not want to do do it one by one manually.

To clarify further, if the user entered any extension I would like to check if it's a valid extension or not as follows:

 if (ext != sysConstants.jpg || ext != sysConstants.jpeg || ext != 
    sysConstants.png) {
   //do smthing
 }

How can I iterate through these constants?

Update:

eclipse highlights the enum with red and says cant resolved to a type

public class EnumTest {

    public enum Ext {
        jpg,
        jpeg,
        png
    }
}
rmaik
  • 1,076
  • 3
  • 15
  • 48
  • 6
    You should use enum, not class fields. – Nikolay Mar 30 '15 at 16:07
  • @rmaik: yes you can declare enum inside class and used it. see [here](http://stackoverflow.com/questions/6280937/java-how-to-access-inner-enum-class) – codiacTushki Mar 30 '15 at 16:13
  • 1
    Once you've set up your enum properly (following the example @rmaik cited, for instance), you can iterate through the instances by using the static values() method on the generated enum class. So, if you call your enum SysConstants, you may use `for (SysConstants syscon : SysConstants.values()) { /* test ext against each enum */}` See [this question](http://stackoverflow.com/questions/13659217/values-method-of-enum). – Jerry Oberle Mar 30 '15 at 16:29

2 Answers2

2

Although you can store your constants in a List or declare an enum for the same you can use reflection to iterate over fields in a class.

Assuming your constants are declared in class as public static final you can do

    for (Field f : YourClass.class.getDeclaredFields()) {
        f.setAccessible(true); // you need this if variables are private
        String name = f.getName();
        String value = (String) f.get(null);
    }

to iterate over all fields.

please also tell me why do u use null

I assumed your constants are static. If not you should use the actual instance/object in the argument. For static members you can use null. As per Java doc

If the underlying field is a static field, the obj argument is ignored; it may be null.
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • please see the updaate section – rmaik Mar 30 '15 at 16:24
  • i used the way u iterate through the class fileds and it works, but when i use f.get(null) to get the value of the field , eclipse want to embed it in try and catch...please alos tell me why do u use null – rmaik Mar 30 '15 at 16:30
  • It has no sense using reflection for a simply question like this... use enums, as people are saying – vzamanillo Mar 30 '15 at 16:34
1

User enum to clarify your code:

public enum ImageTypes {
    JPG, PNG, TIFF 
}

Then you get an image type:

ImageType type = ext;

switch(ext) {
    case JPG:
      // do your stuff...
    break;


    // other cases

    default:
        // error;
    break;

}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109