This post has been edited by Novice.Chimp: 13 April 2009 - 04:16 PM
a question about java local variablehow to access local variable in the other method
Page 1 of 1
2 Replies - 1672 Views - Last Post: 13 April 2009 - 04:21 PM
#1
a question about java local variable
Posted 13 April 2009 - 04:06 PM
hi i need some help, i have a question about how to access the local variable in in one method from the other method. is there anyway that i can do that? if so what command do i need to use? if possible please explain the mechanic of how it works. thank you very much for your time and effort, its much appriciated
(forgot to say, i can't use static variable and the variable i am trying to access is at the method's parameter, though i can probably instantiated the variable at the parameter inside the method hmm)
Replies To: a question about java local variable
#2
Re: a question about java local variable
Posted 13 April 2009 - 04:19 PM
If it's a local variable created inside a method when called, then you can't, unless its the variable returned, example:
localVar goes out of scope as soon as the function returns
If you need to get a value then have whatever you need be returned by the function. However, this really doesn't make any sense because a method is designed to return something (even if its nothing). So any local variable created inside the method can never be accessed from the outside because of scope issues. UNLESS:
edit: Although technically, calling a function with a localVar from another function doesn't really give you the variable itself, only a copy of it.
public getValue(int param)
{
//local to this function
int localVar = 3;
param *= localVar;
return param;
}
localVar goes out of scope as soon as the function returns
If you need to get a value then have whatever you need be returned by the function. However, this really doesn't make any sense because a method is designed to return something (even if its nothing). So any local variable created inside the method can never be accessed from the outside because of scope issues. UNLESS:
void functionOne(int param)
{
//assign localVar to something
//call another function perhaps
int localVar = param *3; //or whatever
functionTwo(localVar); //call do something with it
return;
}
void functionTwo(int var)
{
System.out.println("Local variable from functionOne:" + var);
//do stuff
}
edit: Although technically, calling a function with a localVar from another function doesn't really give you the variable itself, only a copy of it.
This post has been edited by KYA: 13 April 2009 - 04:20 PM
#3
Re: a question about java local variable
Posted 13 April 2009 - 04:21 PM
you just can't
local variables are called local variables because they are local to the method they belong to
if you want them to be shared between multiple methods you have to make them instance variables
local variables are called local variables because they are local to the method they belong to
if you want them to be shared between multiple methods you have to make them instance variables
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|