?
Solved

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

Posted on 2003-11-10
4
Medium Priority
?
170 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
[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
  • 3
4 Comments
 
LVL 15

Assisted Solution

by:jimmack
jimmack earned 200 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 400 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

765 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