[Java] Java 实例变量 局部变量 →→→→→进入此内容的聊天室

来自 , 2019-06-11, 写在 Java, 查看 102 次.
URL http://www.code666.cn/view/7e7e69ea
  1. public class Test {
  2. private int i; //
  3. Test类的实例变量
  4. public int firstMethod() {
  5. int j = 1; //
  6. 局部变量
  7. // 这里能够访问i和j
  8. System.out.println("firstMethod
  9. i="+i+",j="+j);
  10. return 1;
  11. } // firstMethod()
  12. 方法结束
  13. public int secondMethod(float f) { //method parameter
  14. int j = 2; //局部变量,跟firstMethod()
  15. 方法中的j是不同的
  16. //
  17. 这个j的范围是限制
  18. 在secondMethod()
  19. 种的
  20. //
  21. 在这个地方,可以
  22. 同时访问i,j,f
  23. System.out.println("secondMethod
  24. i="+i+",j="+j+",f="+f);
  25. return 2;
  26. }
  27. public static void main(String[] args) {
  28. Test t = new Test();
  29. t.firstMethod();
  30. t.secondMethod(3);
  31. }
  32. }//源代码片段来自云代码http://yuncode.net
  33.                        

回复 "Java 实例变量 局部变量"

这儿你可以回复上面这条便签

captcha