网站搭建网,城市建设局网站,seo实战技巧,网站维护进不去怎么办#xff08;1#xff09; 在Java语言中#xff0c;根据定义变量的位置不同#xff0c;可以将变量分成两大类#xff1b;成员变量和局部变量。 #xff08;2#xff09;成员变量 成员变量包括类变量#xff08;用static修饰的变量#xff09;和实例变量#xff08;不用…1 在Java语言中根据定义变量的位置不同可以将变量分成两大类成员变量和局部变量。 2成员变量 成员变量包括类变量用static修饰的变量和实例变量不用static修饰的变量 一个类在使用之前要经过类加载、类验证、类准备、类解析、类初始化等几个阶段 类变量 类变量在类准备阶段起开始存在直到系统完全销毁这个类。类变量属于类并且与类共存亡类变量在内存中有且只有1份通过类名.类变量 来调用。 实例变量 实例变量在类的实例创建时开始存在直到系统完全销毁这个实例。实例变量属于对象通过对象.实例变量类当用当然类变量也可以使用对象.类变量来访问。 但是需要注意的是通过对象.实例变量调用实例变量的过程中该实例变量仍然属于类而不属于当前的对象 如 package cn.bytecollege.variable; /** 学生类 author Ray * */ public class Student { //静态成员变量 班级 public static String grade; }package cn.bytecollege.variable;/*** 测试类* author Ray**/public class Test {
public static void main(String[] args) {// TODO Auto-generated method stub//为类变量grade赋值Student.grade 三班;//创建对象student1Student student1 new Student();//创建对象student2Student student2 new Student();//通过对象.类变量的方式调用静态变量并修改值 该静态变量属于全体对象student1.grade 一班;System.out.println(student2.grade);//输出一班}
}
3局部变量 1. 分支结构内定义的变量2. 循环结构内定义的变量3. 方法内定义的变量4. 方法参数列表中的变量5. 代码块中定义的变量所有局部变量的作用范围都是离定义变量最近的大括号内如package cn.bytecollege.variable;public class Test1 {
/*** 测试类* param args*/public static void main(String[] args) {// TODO Auto-generated method stub//定义字符数组charArrayString[] stringArray new String[4];//for 循环中的循环变量就是局部变量for(int i0;istringArray.length;i) {if(i%20) {//if分支结构中 s1是局部变量String s1 [(;stringArray[i] s1;}else {String s1 )];stringArray[i] s1;}} //输出数组for(int i0;istringArray.length;i) {System.out.print(stringArray[i]);//输出:[()][()]}}}