Solved

passing objects

Posted on 2012-03-14
1
196 Views
Last Modified: 2012-03-14
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;
}
}
0
Comment
Question by:dpalyca755
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
nareshvoota earned 500 total points
ID: 37723038
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

http://stackoverflow.com/questions/40480/is-java-pass-by-reference

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

Hope this helps.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java program running SQL query 5 68
Cisco ASA: Java web start no go, asdm launcher no go 3 88
program arguments vs VM arguments 4 60
Java pass by reference 3 75
In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question