0

I have this simple class:

public class Events{
    Boolean wentToGym; 
    String eventsToday; 

    public Events(Boolean wentToGym, String eventsToday){
        this.wentToGym = wentToGym; 
        this.eventsToday = eventsToday; 
    } 

Now for some reason I need to iterate through these fields and print them. As they are from different types, I thought I would use the Object declaration in the for-loop:

public static void main(String[] args){
    Events events = new Events(true, "first trial"); 
    for(Object e: events.getClass().getDeclaredFields){
        System.out.println(e.toString); 
    }
} 

Unfortunately not working because the object has no toString method. It gives me the same result you get when you try printing an array. And I dont know how to cast them inside the loop as they are declared as objects. Whats the best way to iterate through the attributes of a class and print them?

Edit: the question some guys rushed into referring to as duplicated is about attributes all of the same type (Strings), so there is no need for using an object nor the problem is the same. Its always a good idea to try helping by even reading the whole thing even if its harder than feeling important by simply flagging a question.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Thank you, but in the case you provided, all the attributes are strings. So there is no problem with iterating nor you need to use the Object. –  Aug 06 '16 at 21:50
  • What do you mean "no `toString()` method"? All variables, save for the primitive types, have `toString()` through inheritance. – Nissa Aug 06 '16 at 21:50
  • Once you declare them as Object in the loop, you can only use the methods in the Object interface. They are no longer treated as Boolean nor Strings –  Aug 06 '16 at 21:52
  • `toString()` is a method on `Object`, at least according to the [docs](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) – ivarni Aug 06 '16 at 21:56
  • The result I get on the Eclipse is the same you get for trying to print an array. You could try it yourself if you have time. –  Aug 06 '16 at 21:59
  • You cast like this: `(NewType)castedObject`. Also, to save memory, use the primitive type `boolean` not the wrapper type `Boolean`. – Nissa Aug 06 '16 at 22:00
  • You do not print the value of the `Field` but the `Field` object itself. You want to get its value for a particular instance with `field.get(instance)`. Look at the linked question. – Tunaki Aug 06 '16 at 22:03
  • java.lang.reflect.Field type has toString() overridden have a look at the java docs. Java has polymorphism so regardless of the reference type if you use toString() runtime result would be as same as Filed type's toString. Of course you won't be able to subclass methods/fields from superclass. The question is not clear – Laksitha Ranasingha Aug 06 '16 at 22:04

1 Answers1

0

Is this what you're looking for?

import java.util.Arrays;

public class Events {

    Boolean wentToGym;

    String eventsToday;

    public Events(Boolean wentToGym, String eventsToday) {
        this.wentToGym = wentToGym;
        this.eventsToday = eventsToday;
    }

    public static void main(String[] args) {
        Events events = new Events(true, "first trial");
        Arrays.stream(events.getClass().getDeclaredFields()).forEach(field -> {
            try {
                System.out.println(field.getName() + ": " + field.get(events));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });
    }
}

Output:

wentToGym: true
eventsToday: first trial
bsferreira
  • 1,189
  • 5
  • 14
  • 27