Solved

Can a Variable be populated from another method in another class.

Posted on 2003-11-10
4
163 Views
Last Modified: 2010-08-05
I've ran across something that I've never seen in Java before.  I step throught somebody's code to get a line of data:
public class VendorMaster
{
next = loaddata.getLine(currentLine);
}

public class LoadData
{
public boolean getLine(byte[] line)
{
...
line is populated here
return gotnext;
}

When I step through the code in VendorMaster after executing getLine, currentLine is populated!  I didn't know you can populate a variable in the called method and make its contents visible in the calling class without actually returning it back in "return" statement.  Why is this happening?  Is this safe to implement especially with having multiple threads?  Thanks.
0
Comment
Question by:Mero
  • 3
4 Comments
 
LVL 15

Assisted Solution

by:jimmack
jimmack earned 50 total points
ID: 9716150
currentLine is passed across as a reference to the beginning of the array, so when the LoadData class writes to it, it's writing to the same memory space.

"currentLine" and "line" are referring to the same block of memory.
0
 
LVL 16

Accepted Solution

by:
imladris earned 100 total points
ID: 9716402
Needing a return statement is true for "primitive" variables (int, long, byte, char etc.). Others (arrays or objects) are passed by reference.

Whether this will pose a challenge w.r.t. multiple threads or not depends on "where" the currentLine is. If it is a shared resource (usually or global or member variable) that multiple threads are accessing, you will need to guard its access with syncronization blocks. However, if currentLine is a purely local, temporary, variable located on the stack, then multithreading will have no impact. Each thread has its own stack, after all.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9716667
Good catch imladris ;-)  I didn't read the last sentence :-(
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9717113
:-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now