How to call a variable in another method in the same class?
public void example(){
String x='name';
}
public void take(){
/*how to call x variable*/
}
First declare your method to accept a parameter:
public void take(String s){
//
}
Then pass it:
public void example(){
String x = "name";
take(x);
}
Using an instance variable is not a good choice, because it would require calling some code to set up the value before take() is called, and take() have no control over that, which could lead to bugs. Also it wouldn't be threadsafe.
x is already being set up," and which would not be thread-safe (if more than one thread was trying to access it). This is the design that he has, and he makes no mention of threads in his post. Instance variables would solve the problem he described.public class Test
{
static String x;
public static void method1
{
x="name";
}
public static void method2
{
System.out.println(+x);
}
}