1、类变量:用static修饰,在类中,其他方法之外。
public class test{
static int num = 0; //类变量(类中,method之外)
public void method(){
}
}
2、实例变量:无修饰符,类中,method之外,随方法调用而生,随方法结束而亡。
public class test{
static int num = 0; //类变量(有修饰符,类中,method之外)
String str = " 实例变量 "; //无修饰符,类中,method之外
public void method(){
}
}
3、局部变量:在类中,且在类的method之中,无修饰符,只有类型。
注:局部变量出生在栈中,没有默认值,因此,在调用时,需要进行初始化。
public class test{
static int num = 0; //类变量(有修饰符,类中,method之外)
String str = " 实例变量 "; //无修饰符,类中,method之外
public void method(){
String name = "局部变量"; //局部变量
int age = 0; //局部变量初始化
age = age + 20;
}
}
路过大佬请指点,小白一起努力。