Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example issue 6 46
how to see all occupied ports on windows 10 laptop 15 76
JavaFX TableView not displaying correctly 3 55
jsp insert to database example 2 45
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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 …

839 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