例        

public class Cow{
private double weight;
public Cow(){};
public Cow( double weight){
this.weight = weight;
}
private class CowLeg{
private double length;
private String color;
public CowLeg(){};
public CowLeg(double length , String color){
this.length = length ;
this.color = color;
}
public void setLength(double length){
this.length = length ;
}
public double getLength(){
return this.length;
}
public void setColor(String color){
this.color = color;
}
public String getColor(){
return this.color;
}
public void info(){
System.out.println( "当前牛腿颜色是:"+color+ ",高:"+length);
System.out.println( "本牛腿所在奶牛重:"+weight);
}
}
public void test(){
CowLeg c1 =  new CowLeg(  1.12 ,  "黑白相间");
c1.info();
}
public static  void main(String[] args){
Cow cow =  new Cow( 378.9);
cow.test();
}
}

显示创建非静态内部类对象来调用访问其实例成员。

public class Outer{
private  int outProp =  9;
class Inner{
private int inProp =  5;
public void accessOuterProp(){
System.out.println( "外部类的outPut值:"+outProp);
}
}
public void accessInnerProp{
System.out.println( "内部类的inProp值:"+  new Inner().inProp);
}
public static void main(String[] args){
Outer out =  new Outer();
out.accessInnerProp();
}
}

如果存在一个非静态内部类对象,则一定存在一个被它寄存的外部类对象。静态成员不能访问非静态成员。 

        外部类的静态方法、静态代码块不能访问非静态内部类,包括不能使用非静态内部类定义变量、创建实例等。总之,不允许在外部类的静态成员中直接使用非静态内部类。

也就是main函数里不能创建非静态内部类实例。

public class StaticTest{
private class In{}
public static void main(String[] args){
// new In();  错误,静态成员不能访问非静态成员。
}
} 

注意,非静态内部类里不能定义静态成员,即静态方法、静态成员变量、静态初始化块。