安全编程(十二) - static

1.引言

        java中被static修饰的成员叫做静态成员或类成员。她属于整个类所有,而非某一个对象所有,即被类的所以对象所共享。静态成员可以使用类名直接访问,也可以使用对象名进行访问。大多时候推荐使用类名进行访问。

        static可以修饰变量、代码、方法块。

        静态成员属于整个类,当第一次使用该类时系统会为她分配内存空间,直到该类被卸载才会回收资源空间。

2.实例

2.1静态变量:

        在一个类中定义一个静态变量:

package cn.nuist.pers.September20;

public class StaticVariable {

	static String str1 = "isleiyi";
	
	public static void main(String[] args) {
	
		System.out.println("1.output:" + StaticVariable.str1);  //1.直接通过类名进行访问,无需创建对象
		
		StaticVariable staVar = new StaticVariable();  //2.创建类的对象
		System.out.println("2.output:" + staVar.str1);     //使用类的对象访问静态变量
		
		staVar.str1 = "newleiyi";  //3.使用对象名的形式来修改静态变量的值
		System.out.println("3.output:" + staVar.str1);  //修改后的静态变量的值
		
	}
}

输出结果:

1.output:isleiyi
2.output:isleiyi
3.output:newleiyi

2.2静态方法:

        用static修饰的方法叫做静态方法,如我们常使用的main也是静态方法。静态方法的使用如下:

package cn.nuist.pers.September20;

public class StaticMethod {

	public static void print() {
		System.out.println("StaticMethod Test");
	}
	public static void main(String[] args) {
		
		StaticMethod.print();  //直接使用类名调用静态方法
		
		StaticMethod staMet = new StaticMethod();  //创建对象调用静态方法
		staMet.print();
	}
}

结果:

StaticMethod Test
StaticMethod Test

2.2.1静态方法中可以直接调用同类的静态成员,但不能调用非静态成员。

package cn.nuist.pers.September20;

public class StaticMethodTest1 {

	String str1 = "helo";  
	static String str2 = "hello";
	public static void main(String[] args) {
		System.out.println("str1 output:" + StaticMethodTest1.str1);  //调用非静态变量
		System.out.println("str2 output:" + StaticMethodTest1.str2);  //调用静态变量
	}
}

结果:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Cannot make a static reference to the non-static field StaticMethodTest1.str1

	at cn.nuist.pers.September20.StaticMethodTest1.main(StaticMethodTest1.java:8)

如果希望在静态方法中调用非静态成员,可以创建该类的对象,然后通过对象来访问非静态成员。

package cn.nuist.pers.September20;

public class StaticMethodTest1 {

	String str1 = "helo";  
	static String str2 = "hello";
	public static void main(String[] args) {
//		System.out.println("str1 output:" + StaticMethodTest1.str1);  //调用非静态变量会出错
		
		StaticMethodTest1 smt = new StaticMethodTest1();   //创建该类的对象来调用非静态成员
		System.out.println("str1 output:" + smt.str1);
		
		System.out.println("str2 output:" + StaticMethodTest1.str2);  //调用静态变量
	}
}

结果:

str1 output:helo
str2 output:hello

2.2.2在普通成员方法中,可以访问到该类中的非静态成员和静态成员。

package cn.nuist.pers.September20;

public class StaticMethodTest2 {

	String str1 = "helo";
	static String str2 = "hello";
	
	public void show() {   //普通成员方法可以访问到静态非静态变量
		System.out.println("1.output:" + str1);
		System.out.println("2.output:" + str2);
	}
}

2.2.3静态方法中不能直接调用非静态方法,需要通过创建对象来调用非静态方法。

package cn.nuist.pers.September20;

public class StaticMethodTest3 {

	public void show() {
		System.out.println("helo");
	}
	public static void print() {
		System.out.println("Helo");
	}
	public static void main(String[] args) {
		StaticMethodTest3 smt3 = new StaticMethodTest3();  //静态方法不能直接调用非静态方法,需要
		smt3.show();                                       //通过对象来调用
		
		print();    //可以直接调用静态方法
	}
}

结果:

str1 output:helo
str2 output:hello

3.总结

多动手,多动脑;活到老,学到老!

参考资料:https://www.cnblogs.com/dianqijiaodengdai/p/6144698.html

猜你喜欢

转载自blog.csdn.net/a_cherry_blossoms/article/details/82788972
今日推荐