• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing objects to other objects  RSS feed

 
Ryan Chidley
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a single-threaded application. In that application there is a class C which needs to be aware of all changes in the fields of class A, whenever and wherever in program execution those changes in A occur. Typically, I'll pass A to C's constructor, then I'll pass A to B,E,F,G and other classes. The fields of A will change in B,E,F,G etc. I'm doing something like the below when I pass A to C:

public void CClass {

private AClass A;

public void receiveAClass(AClass A) {
this.A = A; //option1

}
}

My first question is: when I circle back to C, after changing A in class B,E,F,G etc., will C.A have the most recent field values?

My next question is: if I wanted to just send a copy of A's data at that point in time, What is the best way to pass and assign just a snapshot of A instead of a dynamically-updating-reference-to-A to C?

Thank you very much for your time.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, objects aren't passed, references are.

My first question is: when I circle back to C, after changing A in class B,E,F,G etc., will C.A have the most recent field values?

What were your observations when you tried it?

What is the best way to pass and assign just a snapshot of A instead of a dynamically-updating-reference-to-A to C?

Create a new instance of A with that state and pass that reference instead. This is often done via a copy constructor or the clone() method.

And welcome to the Ranch!
 
Ryan Chidley
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"What were your observations when you tried it?"

My program is large, and I am debugging the logic. I'm trying to know if there are any circumstances where I can do something to A in other classes which somehow won't be known in C. I can't think of anything, but I am not an expert like you all.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Chidley wrote:I'm trying to know if there are any circumstances where I can do something to A in other classes which somehow won't be known in C.


Is this a question about classes which you have written? Since we haven't seen any code for these classes, it's impossible for us to answer any such questions about the code. And since you said

My program is large, and I am debugging the logic.


it does look to me as if you're asking a question about code you have written.

But I could certainly be misunderstanding your question. Perhaps you could clarify, or ask the question differently? (Also I have to say that I have trouble discussing classes named A and C. I find it more useful to discuss real classes which are designed to solve real problems. But maybe that's just my problem.)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Chidley wrote:"What were your observations when you tried it?"

My program is large, and I am debugging the logic. I'm trying to know if there are any circumstances where I can do something to A in other classes which somehow won't be known in C. I can't think of anything, but I am not an expert like you all.



I think that the point that Darryl was making is... it is very easy to write a simple test program to answer your question. And the test program can probably be done faster than it takes to actually wait for an answer in a forum.

I understand that you are not an expert -- but it is hard to become an expert, if you are not willing to just try out stuff (even if you are wrong, as you can learn what not to do).

Henry
 
Ryan Chidley
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"I think that the point that Darryl was making is... it is very easy to write a simple test program to answer your question. And the test program can probably be done faster than it takes to actually wait for an answer in a forum.

I understand that you are not an expert -- but it is hard to become an expert, if you are not willing to just try out stuff (even if you are wrong, as you can learn what not to do). "

I hear you. You are right. I'm just not 100% sure the test program will definitively settle the issue for me.

Thank you for your time all.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Chidley wrote:I hear you. You are right. I'm just not 100% sure the test program will definitively settle the issue for me.


Not a problem... if you still have a question, we're still here to answer questions.

(However specific questions are better than general questions, and questions about specific pieces of code are even better.)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!