How to call a variable in another method?

前端 未结 4 1022
暖寄归人
暖寄归人 2021-01-17 00:04

How to call a variable in another method in the same class?

public void example(){    
    String x=\'name\';
}

public void take()         


        
4条回答
  •  粉色の甜心
    2021-01-17 00:25

    Since they are in different scopes you can't.

    One way to get around this is to make x a member variable like so:

    String x;
    
    public void example(){
        this.x = "name";
    }
    
    public void take(){
        // Do stuff to this.x
    }
    

提交回复
热议问题