Get your CodeRanch badge!*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Local Variable" Watch "Local Variable" New topic
Author

Local Variable

Susilo Saja
Ranch Hand

Joined: May 27, 2003
Posts: 91
Hi,
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.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
NO.
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
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

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: http://aspose.com/file-tools
 
subject: Local Variable
 
Similar Threads
Dan's questions : static variable
Doubt regarding Method Local Inner Class
why this with static
Question to an output
access of non final variable from method local inner class