Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6000293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:40:22+00:00 2026-05-23T00:40:22+00:00

I have two classes. The first class has someField which the second class needs

  • 0

I have two classes. The first class has someField which the second class needs to change, so the first class calls FirstClass.setSomeField(value). The second class can not change the field at that moment, so it stores this FutureChange into a list which it will go through at a later time and apply the necessary changes. The class must store it in a way that it can compare instances of FutureChange, i.e. to avoid setting the same field twice, and other things.

What is the best way to implement FutureChange? With reflection, this is easy. The problem is that I will have hundreds of classes, each class with many fields.

I even tried a map, with a switch for field names:

public class Example { 

    public static Integer number;
    public static String words;

    public static void main(String args[]) {
        Map<Field, Object> map = new HashMap<Field, Object>();
            // store modifications
        map.put(Field.number, new Integer(10));
        map.put(Field.words, "Words");
        // some time later
        for(Map.Entry<Field, Object> entry: map.entrySet()) {
            modify(entry.getKey(), entry.getValue());
        }
    }

    public static void modify(Field m, Object value) {
        switch(m){
        case number:
            number = (Integer) value;
            break;
        case words:
            words = (String) value;
            break;
        }
    }

    public enum Field {
        number,
        words;
    }
}

But this isn’t the most effective way, and could get clogged up as the classes get larger. Anyone know how to best implement this?

Some more code:

public class Main {
    FirstClass a = new FirstClass();
    SecondClass b = new SecondClass();
    // Third, Fourth, etc.

    public static void main(String args[]) {
        while(true) {
            // run each, no modifications allowed
            a.run(); // may want to modify several variables of b
            b.run(); // same as above
            // end of all runs, now modify
            a.modifyAll();
            b.modifyAll();
        }
    }
}

As you can see, I must store it during the runs, and execute later.

Storing anonymous inner Callable and calling later would be perfect, but I need to examine what it is doing so that I can compare two and determine whether they are conflicting.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:40:23+00:00Added an answer on May 23, 2026 at 12:40 am

    I would use reflections as you suggested. This is going to be much slower than applying the changes directly. If you really want efficiency, you are better off breaking your code into two phases/stages one which uses the current value and one which applies the changes rather than trying to store the changes.

    public class StoredChanges {
        private final Map<Object, Map<String, Object>> changes = new IdentityHashMap<Object, Map<String, Object>>();
    
        public void storeChange(Object o, String fieldName, Object value) {
            Map<String, Object> changedForObject = changes.get(o);
            if (changedForObject == null)
                changes.put(o, changedForObject = new LinkedHashMap<String, Object>());
            changedForObject.put(fieldName, value);
        }
    
        public void applyChanges() {
            for (Map.Entry<Object, Map<String, Object>> objectMapEntry : changes.entrySet()) {
                Object o = objectMapEntry.getKey();
                for (Map.Entry<String, Object> fieldChange : objectMapEntry.getValue().entrySet()) {
                    String fieldName = fieldChange.getKey();
                    try {
                        Method setter = o.getClass().getMethod("set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1));
                        setter.invoke(o, fieldChange.getValue());
                    } catch (Exception e) {
                        e.printStackTrace(); // or log it.
                    }
                }
            }
            changes.clear();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes. First class has TabPage control. I want to change layout
Let's say I have two classes. Each class has one parameter. Parameter of first
Suppose that I have two classes, first a class without any properties, fields or
Hi I work with netbeans. I have written a code which has two classes
I have an application with two classes, A and B. The class A has
I've got two generic base classes. The second generic class has a constraint on
I have two classes, the first called Radish and the second called RadishCont. All
First I want to describe my situation briefly. I have two classes, one MainClass
I have two classes, and want to include a static instance of one class
I have two classes, Foo and Bar, that have constructors like this: class Foo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.