5 Replies - 4904 Views - Last Post: 19 January 2010 - 06:02 PM Rate Topic: -----

#1 cdarklight  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 70
  • Joined: 15-August 09

passing a class object into another class?

Posted 19 January 2010 - 11:33 AM

I have two classes, both are instantiated in the main. I want to pass one class instantiation to be stored and used in the other class.
if I wanted to do something like this, how would it work? Both syntactically and within the program.
Please and thank you

public class A{
	public A(B something){ 
		// what would go here to pass a preexisting item 
		// 'something' of class 'B' into the 'A' class?
	}
	private B myBobject;
}



Could using

myBobject = something;

work without causing possible issues?
what would be the most stable way to do this?

Thanks again to anyone who helps. I'm mostly frustrated because I can do this in c++ by passing a reference. I'm just now learning java and it's confusing in its similarities and differences. I try not to compare the two but its so hard.

Is This A Good Question/Topic? 0
  • +

Replies To: passing a class object into another class?

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: passing a class object into another class?

Posted 19 January 2010 - 11:51 AM

Since they're both in main, you don't want any changes in B to affect A's copy of it correct? So, when you pass in B, clone it and assign it to A's data member.

public class A{
	public A(B something){
		myBobject = something.clone();
	}
	private B myBobject;
}


Was This Post Helpful? 1
  • +
  • -

#3 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2729
  • View blog
  • Posts: 15,806
  • Joined: 24-September 08

Re: passing a class object into another class?

Posted 19 January 2010 - 11:52 AM

You can create the classes instantiation variable into a constructor, yes, and use it, just as long as your constructor recognizes it as a parameter.
Was This Post Helpful? 0
  • +
  • -

#4 cdarklight  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 70
  • Joined: 15-August 09

Re: passing a class object into another class?

Posted 19 January 2010 - 11:55 AM

Thank you! I like the bit about the clone.
Was This Post Helpful? 0
  • +
  • -

#5 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2729
  • View blog
  • Posts: 15,806
  • Joined: 24-September 08

Re: passing a class object into another class?

Posted 19 January 2010 - 12:18 PM

Then click the "this post was helpful" button at the bottom of the helpful post ^__^
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10649
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: passing a class object into another class?

Posted 19 January 2010 - 06:02 PM

View PostKYA, on 19 Jan, 2010 - 02:51 PM, said:

Since they're both in main, you don't want any changes in B to affect A's copy of it correct? So, when you pass in B, deep clone it and assign it to A's data member.


Fixed. If B contains any non-primitive attributes, you should make sure you create a new B object (should be done anyways) and copy the values from all the attributes from the original into the new object. If you just create a shallow copy, then any reference-type attributes in the new object will point to the same objects as in the original object. So for example, if this is the format of B and you want to clone it, you will have to copy all the elements from list into the LinkedList in the B clone. Otherwise, the LinkedList in the B clone will point to the LinkedList in the original B object.
class B{
   LinkedList list; 
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1