Solved

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

Posted on 2003-11-10
4
167 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

713 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