Unable to access a method's local variables outside of the method in Java

后端 未结 4 1052
遥遥无期
遥遥无期 2020-12-20 09:44

I am new to Java and am trying to access method variables outside of the method, but it doesn\'t work.

Code is below:



        
4条回答
  •  余生分开走
    2020-12-20 10:15

    Because a and b are local variables. If you want to access to them in your main method, you need to modify your code. For example :

    public class methodacess {
           private static int a;
           private static int b;
    
           public static void minus(){   
               methodacess obj =new methodacess();
               a=10;
               b=15;    
           }   
    
           public static void main (String[] args){     
               int c = b - a;    
           }   
    } 
    

提交回复
热议问题