faint~~~ Rectangle myRect只申明了个Rectangle变量而已,没有构造对象怎么可以访问其实例变量?Rectangle myRect=new Rectangle();

解决方案 »

  1.   

    同意楼上所述。
    具体的说,引用变量的生成过过程虽然就一句话:
    如:
    Rectangle myRect=new Rectangle();
    还是分几个过程的。
    1.在new之前,myRect只存在于栈内存中。
    2.new之后,开始在堆内存中产生变量的属性定义,先是java环境对属性类型的默认赋值:
    如int的默认值是0,String的默认值是null等。
    3.然后才是构造方法的赋值。
      

  2.   

    public class SomethingIsWrong {
    public static void main(String[] args) {
      Rectangle myRect = new Rectangle();
      myRect.width = 40;
             myRect.height = 50;
             System.out.println("myRect's area is " + myRect.area());
        }
    }
    看看.