2

I have three classes. I want to use all classes in one function from another classes. I need to collect all Letter class objects and put the result from getName and getWeight from each object in a HashMap. I need to this with Package and Deliverer as well.

Already tried to use getDeclaredField/getDeclaredClasses.

import java.lang.reflect.Field;

public class Parcel
{
    protected static void addWeight()
    {
        Class<?>[] classes = Letter.class.getDeclaredClasses();
        for (int i = 0; i < classes.length; i++) {
            System.out.println("hello");
            System.out.println("Class = " + classes[i].getName());
        }

        Field[] allFieldsLetter = Letter.class.getDeclaredFields();
        for (int i = 0; i < allFieldsLetter.length; i++) {
            double value = Letter.getWeight();
            Delivery.weightList.put(Letter.getName(), value);
        }
        Field[] allFieldsPackage = Package.class.getDeclaredFields();
        for (int i = 0; i < allFieldsPackage.length; i++) {
            double value = Package.getWeight();
            Delivery.weightList.put(Package.getName(), value);
        }
        Field[] allFieldsDeliverer = Deliverer.class.getDeclaredFields();
        for (int i = 0; i < allFieldsDeliverer.length; i++) {
            double value = Deliverer.getWeight() * 5;
            Delivery.deliverer.put(Deliverer.getName(), value);
        }
    }
}

If I create 3 Deliverer objects for example, I want to get 3 three names, one for each object, and their weights in a HashMap (Delivery.deliverer.put(Deliverer.getName(), value);).

3
  • 3
    Your code doesn't make much sense, you are using for loops but you are not accessing values from any array or any other type of collection inside them and you talk about having 3 objects of a type but all your code uses static methods and variables. Is the hashmap supposed to be a static instance for each class or a member variable or perhaps a global collection for all object types? Commented Oct 29, 2019 at 12:31
  • 1
    I don't think you mean to be using the reflective classes/methods for class and fields. Your comments suggest you mean to be doing something much more normal like creating list of objects and then looping over them. I'm I on the right track? Commented Oct 29, 2019 at 13:21
  • Your on the right track fedup. Commented Oct 29, 2019 at 15:23

1 Answer 1

1

Here is a small example where Delivery has a constructor that accepts the name and the value of weight as a parameter.

List<Delivery> myDeliveryInstances = new ArrayList<Delivery>();

myDeliveryInstances.add(new Delivery("D1", 10));
myDeliveryInstances.add(new Delivery("D2", 25));
myDeliveryInstances.add(new Delivery("D3", 50));

Map<String, Long) deliverer = new HashMap<String, Long>();
for (Delivery delivery : myDeliveryInstances) {
    deliverer.put(delivery.getName(), delivery.getWeight());
}
Sign up to request clarification or add additional context in comments.

1 Comment

When I get home will try this solution, seems to be a pretty good one ;)

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.