Java static

xiaoxiao2021-02-27  417

static只能修饰类里的成员,不能修饰外部类,不能修饰局部变量、局部内部类。

如果不使用static修饰类里的成员,那么这些成员属于该类的实例,如果使用了static修饰,这些成员就属于类本身。

java类里定义成员变量时表面上没有先后顺序,但是实际上java要求定义成员变量必须采用合法的前向引用。

/** * @Description: * @author mymilkbottles xz_mymilkbottles@163.com * @date 2017年5月4日 上午10:37:47 * @version V1.0 */ package Java20170504; /** * @Description: * @author mymilkbottles xz_mymilkbottles@163.com * @date 2017年5月4日 上午10:37:47 * */ public class Def { private class A{ //num2 cannot be resolved to a variable int num1=num2+2; int num2=10; } private class B{ //Cannot reference a field before it is defined static int num1=10+num2; static int num2=20; } private class C{ int num1=num2+10; //The field num2 cannot be declared static in a non-static inner type, //unless initialized with a constant expression static int num2=10; } private class D{ //right int num1=num2+10; static final int num2=10; } }
转载请注明原文地址: https://www.6miu.com/read-1815.html

最新回复(0)