1

I am trying to created a commonMethod which acccepts a Generic List allData type and prints out the values of the alldata object received. Additionally, I am sending the className as a parameter so that we can retrieve the Class and the Fields through Reflection.

I am almost there except the part where I want to declare the className in a forloop. I know the rest of the code works, because i tried hardcoding a ClassName and it works.

public void commonMethod(List<?> alldata, String className) {
      Class c = Class.forName(className);
      Field[] fields = c.getDeclaredFields();

     //I would like to define incoming className here..but failing   
      for (className c : alldata) {
          for (Field field : fields) {
                field.setAccessible(true);
                Object value = null;
                value = field.get(c);
                System.out.println(value);
                //use the value in some other code
           }  
      }

 }

How do I change this part for (className c : alldata) to get it working..

Thanks

2
  • 5
    for (Object c: allData). Commented Jun 25, 2017 at 7:35
  • 1
    You could type the method: public <T> void commonMethod(List<T> alldata, Class<T> c) { /* removed: Class c = Class.forName(className); */ then use T Commented Jun 25, 2017 at 7:37

4 Answers 4

2

You can't do it the way that you want, because declaring variables to be a certain type like that (typeName variableName) is reserved for type names that you know at compile-time.

The proper way is to use:

for (Object c : alldata) {

field.get(c) will work just fine with this, since the get method takes an Object, so c will have a good enough type.

Sign up to request clarification or add additional context in comments.

Comments

1

Change the generic type of the parameter List to be concrete instead of ?, e.g. List<T> alldata.

Comments

1

How about this:

public <T> void commonMethod(List<T> alldata) {
    // do something
}

1 Comment

Sure; but this gives no advantage over ? and Object.
1

I suggest you modify your code as below to accommodate for the generics

public <T>void commonMethod(List<T> alldata) {
      for (T c : alldata) {
          .... 
          .....
      }

 }

Now Whatever class you supply, The compiler would easily understand what class it require to cast variable c into.

Still better is what is suggested by RC in the comments :

public <T> void commonMethod(List<T> alldata, Class<T> c) {
      Field[] fields = c.getDeclaredFields();

      for (T c : alldata) {
          for (Field field : fields) {
                field.setAccessible(true);
                Object value = null;
                value = field.get(c);
                System.out.println(value);
                //use the value in some other code
           }  
      }

 }

1 Comment

The first part of the answer is unnecessary: just change the loop variable type to Object; you get nothing from the type variable other than a shorter type. On the other hand, the second part is good. The only thing I'd change would be to make it Class<? super T> (or List<? extends T>), to make it a tiny bit more flexible.

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.