How to call a variable in another method?

前端 未结 4 1118
暖寄归人
暖寄归人 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

    You make it an instance variable of the class:

    public class MyClass
    {
        String x;
    
        public void example(){ x = "name"; } // note the double quotes
        public void take(){ System.out.println( x ); }
    }
    

提交回复
热议问题