05_javaSE面试题:成员变量和局部变量

题目

/**
 * 类变量:static修饰的
 * 实例变量:不是static修饰的
 *
 * 局部变量:栈
 * 实例变量:堆
 * 类变量:方法区
 * @author kevin
 * @date 2019/7/11 9:41
 */
public class Exam5 {
    static int s;//成员变量,类变量
    int i;//成员变量:实例变量
    int j;//成员变量:实例变量
    {
        int i =1;
        i++;
        j++;
        s++;
    }

    public void test(int j){
        j++;
        i++;
        s++;
    }

    public static void main(String[] args) {
        Exam5 obj1 = new Exam5();
        Exam5 obj2 = new Exam5();
        obj1.test(10);
        obj1.test(20);
        obj2.test(30);
        System.out.println(obj1.i+","+obj1.j+","+obj1.s);
        System.out.println(obj2.i+","+obj2.j+","+obj2.s);

    }
}

分析

局部变量和成员变量的区别

- 局部变量:方法体中,形参,代码块{} 中
-  成员变量:类方法外;类变量:static修饰 ;实例变量:没有static修饰
-  存储位置
	-  局部变量:栈
    - 实例变量:堆
	- 类变量:方法区

画图分析

结果

2,1,5
1,1,5
原文地址:https://www.cnblogs.com/zhenghengbin/p/11168568.html