How to call a variable in another method?

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.

Leave a Comment