• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

passing objects

If I instantiate an object in my application's main class, and then pass it to the constructor of another class which will need to use it, does the copy/assignment of the object to something local actually make another object?
Generic example below:
Class A {
ClassB B = new ClassB();
ClassC C = new ClassC(B);

Class C {
ClassB localB = null;
Class C (ClassB B) {
  localB = B;
1 Solution
Java is always pass by value but when you pass a reference to a method, the reference gets copied over. But it still points to the old object.

Please read this discussion in stackoverflow


In your case,  localB is referring to the new class B object that is made in class A.

Hope this helps.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now