为什么要把print()定义在后面?
放在前面试试!
static是只能有一个!你的程序出错原因就在这里!

解决方案 »

  1.   

    静态方法中只能使用静态变量,静态方法。非静态变量,静态方法使用前先实例化。
    class A 
    {
    public static void main(String[] args) 
    {
    System.out.println("Hello World!");
    A test = new A();   //建立一个该类的实例
    test.print();       //调用该实例的方法
    }
    public void print()
    {
    System.out.println("hello!");
    }
    }
    即可。
      

  2.   

    class A 
    {
    public static void main(String[] args) 
    {
       A a = new A();
       a.print();
    }
    public void print()
    {
    System.out.println("hello!");
    }
    }
      

  3.   

    to:21bird(世纪菜鸟) 
    1。方法定义在前后并无关系。
    2。static方法的数目是不限的。
      

  4.   

    可以啊,我编译就能通过...没有理由不行的啊。static就表示所以此类型的类空间中只生成一个实例。
      

  5.   

    晕,原来在main里还调用了print,这当然是不行的。。
    静态的怎么能调用非静态函数,它又不知道是哪个实例的。
      

  6.   

    to:clane(clane) 
    不可能。
    静态方法中不能直接使用非静态方法。
    main就是典型的静态方法。
      

  7.   

    怎么这么笨啊?这可是最基本的概念呀!!!
    static方法里面只能调用static的变量,方法。你要说出道理?那你先回答我为什么1+1偏要等于2哪?
      

  8.   

    >>>coaa
    panq()说的是对的。
    静态方法中只能使用静态变量,静态方法。
    因为所谓静态就是类的所有实例公用一个方法,
    不管你new了多少个实例,静态方法只有一个。
    因此,如果你在静态方法中调用了静态变量或者静态方法的话,它没办法知道是属于哪一个实例的啊!因此就要出错!
    还有,静态变量和静态方法的个数是没有限制的!
    跟c++差不多的!
      

  9.   

    不好意思,让大家见笑了!
    看来,我以前看的java全都就饭吃了!
    不过,都是在学习嘛!重在搀和!@_@
    说错了,不是还有大虾么!
    希望大家别见怪!
      

  10.   

    to panq():static就表示所以此类型的类空间中只生成一个实例!这句不对么?
    是误会了我说的吧?
    是我说的不太明确!
      

  11.   

    A a = new A();
      a.print();
    panq()说的是对的。
      

  12.   

    static 有点像C/C++中全局变量,但还是不一样……
    static 定义的变量和方法不需要生成类的实例就可以直接使用,所以改为public static void ……就可以的。 如果去掉static 则是一个实例方法,需要实例化后才能使用,于是
    A a= new a();  // 对象实例化
    a.print();  // 调用
    搞定!
      

  13.   

    :21bird(世纪菜鸟) 
    是误会了
      

  14.   

    class A 
    {
    public static void main(String[] args) 
    {
    System.out.println("Hello World!");
    A test = new A();  
    test.print();      
    }
    public void print()
    {
    System.out.println("hello!");
    }
    }
    是也!
      

  15.   

    两种方法:
    1、方法一
    class A 
    {
    public static void main(String[] args) 
    {
    System.out.println("Hello World!");
    A test = new A();  
    test.print();      
    }
    public void print()
    {
    System.out.println("hello!");
    }
    }
    2、方法二
    class A 
    {
    public static void main(String[] args) 
    {
    System.out.println("Hello World!");
    print();      
    }
    public static void print()
    {
    System.out.println("hello!");
    }
    }这下该明白了吧!


    !!!


    !!


    !!

    !!


    !!




    !!
      

  16.   

    static 方法只能调用static 方法
    想调用非static 方法必须创建对象!
      

  17.   

    main方法必须是static的,
    其它方法也可以是 static.
      

  18.   

    这个问题哦,
    static 是程序的入口的概念吧...
      

  19.   

    同意:: Tarloy() 
    static 一项重要的用途就是帮助我们在不必创建对象的前提下调用那个方法.
          这一点是至关重要---特别是在定义程序运行入口方法main()的时候.
    例如:你的例子可以这样改:
    class A 
    {
    public static void main(String[] args) 
    {
    System.out.println("Hello World!");
    A.print();      
    }
    public static void print()
    {
    System.out.println("hello!");
    }
    }
      

  20.   

    使用static可以直接使用类中的方法,否则需要通过对象调用方法。
    static用在类上有上述效果。
    用在变量上,只需对该变量初始化一次。
      

  21.   

    21bird,你那200分的影响不小啊!引来这么多大侠
      

  22.   

    我操,那么简单的问题,还要问阿?
    你要明白static方法不是属于对象的,所以在static的方法中无法调用类的方法!
      

  23.   

    to davidweimin(小虫):
       您搞错啦!
       这个帖子不是我发的!
       如果我舍得200分的话,我的问题也许不会有那么多没解决了!^&^