package 面向对象;
public class BLdemo {
/**
* @局部变量和成员变量的区别:
* 1,作用范围
* 成员变量:定义在整个类中,用于整个类
* 局部变量,定义在方法中,只在它所属的范围内有效
*
* 2,存储区域不同
* 成员变量:存储在堆内存中,随着对象的存在而存在,但对象变成垃圾,会被回收
* 局部变量,存在栈内存中,当方法被调用才存在方法执行完立刻释放
*
* 3,初始化情况
* 成员变量:有默认的初始化值
* 局部变量:没有默认的初始化值,使用前必须进行初始化.
*/
//成员变量
int num =10;
String str;
int age ;
public void show(){
System.out.println(num);
System.out.println(age);
//不在有效的作用范围内,不能被使用
//System.out.println(num1);
}
public static void main(String[] args) {
// 局部变量
int num1 =10;
String str1;
BLdemo d = new BLdemo();
d.show();
System.out.println(d.str);
//没有进行初始化
//System.out.println(str1);
System.out.println(num1);
//进行一下初始化
str1 = "GGG";
System.out.println(str1);
}
}
局部变量和成员变量的区别
最新推荐文章于 2023-03-28 21:04:43 发布