File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Local Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Local Variable" Watch "Local Variable" New topic

Local Variable

Susilo Saja
Ranch Hand

Joined: May 27, 2003
Posts: 91
Is it possible for a Thread to access a local variable created by other Thread?
For example, Thread x access a method and create a local variable. Thread x has not finished, but then Thread y access the same method and also create a local variable. Is it possible for Thread y to access the local variable created by Thread x?
Dana Hanna
Ranch Hand

Joined: Feb 28, 2003
Posts: 227
Quite simply, no.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
local variables and method arguments (including the object "references") are kept in stack! and each method call gives rise to another include to the stack.
Idly Vada
Ranch Hand

Joined: Sep 02, 2003
Posts: 135
If the variable is declared using ThreadLoacal class, it is specific to the thread. Otherwise(I think) we can access thread variables.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The purpose of the ThreadLocal class is indeed to create variables that are specific to a thread, but it's used for member or static variables. As has already been (correctly) stated in this thread, the local variables of a method, static or not, are unique to that single method invocation, and are never shared with any other invocation on the same or any other thread.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Local Variable
It's not a secret anymore!