0

I'm trying to modify an object within a class, but have those changes persist up to the class that called this class and passed the object. Here's some code to make it more clear.

I have a class called AddCourseForm, here's its constructor:

    public AddCourseForm(CourseData c) {
        addOrEdit = 0; //default add mode
        initComponents();
        courseList = c;
    }

where courseList is aprivate CourseData courseList;

What I want to be able to do is use the courseList in my AddCourseForm class (add, remove things) but have those changes persist to the CourseData c that's passed to the constructor, but I'm not really sure what to do here. I know how I can make the changes persist if I'd passed CourseData c directly to a method, but not how to do it this way.

4
  • Yoy want the parameter will changed out of function too? Commented Nov 24, 2013 at 3:24
  • 2
    Since you save a reference to c (courseList) anytime you change courseList, c will also be changed as well. They point to the same object. Is this what you want? Commented Nov 24, 2013 at 3:27
  • What is it from CourseData that you want to persist? Commented Nov 24, 2013 at 3:29
  • @user1896769 That's exactly what I want. It would seem I'm doing it correctly then! Probably just some small typo that's giving me trouble at this point :) Commented Nov 24, 2013 at 3:52

1 Answer 1

1

All you need to do is to set values in the CourseData object that you pass to your AddCourseForm class.

Consider the following example:

public class RefTest {

    private static class Ref {
        /* Don't do this in real life, public fields are bad practice.
         * just done here for sake of example.
         * make a getter and setter at the very least.
         */
        public int someInt;  
    }

    private static class RefMod {
        private Ref r;
        public RefMod(Ref r) {
            this.r = r;
        }

        public void setRefInt(int newInt) {
            r.someInt = newInt;
        }
    }

    public static void main(String[] args) {
        Ref ref = new Ref();
        ref.someInt = 11;

        System.out.println(ref.someInt);

        RefMod refMod = new RefMod(ref);
        refMod.setRefInt(42);

        System.out.println(ref.someInt);

        final Ref fRef = new Ref();
        fRef.someInt = 300;

        System.out.println(fRef.someInt);

        refMod = new RefMod(fRef);
        refMod.setRefInt(500);

        System.out.println(fRef.someInt);
    }
}

The output will be:

11
42
300
500

The CourseData that you pass in your constructor is just a reference to an object. That reference will not change in the caller. The data fields contained in the referenced CourseData object are able to change assuming they are mutable and visible.

You'll notice that even though the second Ref in my example, fRef is declared as final it's someInt field can still be modified.

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

3 Comments

If I understand you correctly, that would mean that I'm already doing it correctly. If I have public void foo(ArrayList list) and call list.add() inside foo(), I know list will have those changes once the method returns. What I'm doing is essentially the same thing? So if I call courseList.add() anywhere in the class, the c from the constructor will also get those changes?
@BhargavB Yes you are already doing it correctly. Note that if you change AddCourseForm#courseList to refer to something else or change the outside ref to something else they would then refer to different objects and changes will not be reflected. "Sharing" an object like this only works while the two references still refer to the same object. (Should be obvious but some have trouble with the concept at first.)
Oh yeah, definitely. I'm very comfortable with the concepts at hand here, I just wasn't sure how that would apply to changing objects across classes via constructor referencing. Thanks for all the help :)

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.