类变量:被static修饰的变量。
类类型变量:用类定义的变量。如:Person p=new Person();
static 用法总结
1
当某个变量的值不需要再变化,让所有对象共享同个内存上的变量,以节省内存空间。
  static int x=9
2
Static变量特点:
随着类的加载而加载,优先于对象存在‘
被所有对象共享,可以直接用类名调用,当然也能通过new对象来调用。
非静态变量随着对象的产生而产生,随着对象的消失而消失,因此其生命周期比类类型变量长。
共享区中有:静态变量,方法。
 
3 使用注意事项:
静态方法只能访问静态成员,当静态方法相访问非静态成员时,只能通过对象引用来访问。每个非静态方法里面都存在一个本类对象this引用,因为出于对象的引用的需要。注意,非静态方法可以访问静态成员和非静态成员。
静态方法中不能使用thissuper关键字。因为this指向对象,但静态方法和类比对象先加载。
 
举例:静态代码块:
特点:随着类的加载而加载,并只执行一次,,用于类的初始化,而构造函数给对象初始化。实例代码块随着对象的建立而加载,建立几个对象就加载几次。
class A
{
static
{ System.out.println(d);                      }
 
}  
show()
{
       System.out.println(g);
 
}
}
 
class B
{
       static
{
}
   
{
       B b=New B();
   B b=New B();
// A aa=null;  //注意建立对象的引用为空时不加载类的。
 
}
       static
{
System.out.println(a);
}
 }
函数的执行结果为 b
                 a
                 d
                 c
 
4 内部类:就是定义在类中的类。
特点:内部类可以直接访问外部类的成员。但外部类要访问内部类要通过对象引用来访问。
注意:内部类只有定义在成员位置上才能被private等关键字修饰,本例子中就是和x处于同等位置上。
格式如下:
{
       private Int x=3;
       // r //这里私有化很有必要,为了避免被外界直接访问,并对外提供方法访问。
Private Class Inne
{
void show()
{System.out.println(x);}
}
 
void method(0
{
       Inner in=new Inner();
       In.show();
 }
}
 
class InnerDemo
{
       Public static void main(String[] args)
{
       Outer out =new Outer();
       Out.method();
//  Outer.Inner in= new Outer().new Inner();
      in.show();这种表达方式不提倡,因为内部类往往会私有化。
}
 
 
什么时候内部类会被定义成static
一般不定义为静态,一定为静态就必须将其他成员变量静态,否则无法使用,
当内部类定义为静态时,可以通过以下方式访问
new Outer.Inner().show();  但前提前提是内部类非私有的。
 
 
 
内部类可以直接访问外部类的成员,因为内部类持有外部类的应用。
class Outer
{
       private Int x=3;
private Class Inne
{   int x=4;
       Void show()
{
Int x=5;
 
}
}
想打印4时,System.out.println(this.x);
想打印3时,System.out.println(Outer.this.x);
 
 
内部类做局部内部类
 
class Outer
{
       private  int x=3;
void method()
{
final Int y=9
       class InnerDemo
{
   void show()
   System.out.println(y);//不能直接访问局部变量y,除非final修饰。但仍可以访问成员变量x
}
      
Inner in = new Inner();
in.show();;
}
}
 

class   InnerDemo{

       public static void main(String[] args)
{
 Outer out =new Outer();
       Out.method();
     }