import java.io.*;
public class myapp3 
{
int balance =9;
public static void main(String[] args) 
{
getB();
}
int getB()
{
return this.balance;
}}
编译报错:
myapp3.java:8: non-static method getB() cannot be referenced from a static conte
xt
                getB();

解决方案 »

  1.   

    静态方法里只能访问静态资源,把int getB()方法改成静态int static getB()就可以了
    或者把
    public static void main(String[] args) 
    {
    getB();
    }
    里的调用改成:
             public static void main(String[] args) 
    {
                      myapp3 myapp=new myapp3();
    myapp.getB();
    }
      

  2.   

    getB();
    必须是static 才能在 static  void main()中调用,这是基本基本基本概念
      

  3.   

    我改成这样:
    import java.io.*;
    public class myapp3 
    {
    int balance =9;
    public static void main(String[] args) 
    {
    getB();
    }
    int static getB()
    {
    return this.balance;
    }
    }
    编译还是报错:
    myapp3.java:9: <identifier> expected
            int static getB()
                ^
    myapp3.java:13: ';' expected
      

  4.   

    java
    程序里是不是不可以定义那么多静态方法呀?
    import java.io.*;
    public class myapp3 
    {
    int balance =9;
    public static void main(String[] args) 
    {
                      myapp3 myapp=new myapp3();   
    myapp.getB();
    }
    int static getB()
    {
    return this.balance;
    }
    }
      

  5.   

    三楼的兄弟,不一定“必须是static 才能在 static  void main()中调用”,以下的调用也可以阿:
    import java.io.*;
    public class myapp3 
    {
    int balance =9; public static void main(String[] args) 
    {
    num2 num = new num2();
    num.print();
    }
    int getB()
    {
    return this.balance;
    }
    }class num2
    {
    num2()
    {
    System.out.println("执行构造函数");
    }
    void print()
    {
    System.out.println("*");
    }
    }
      

  6.   

    声明时应该是做用域放前面,类型放后面
    static int getB()
    {
    ....
    }
      

  7.   

    依然报错:
    myapp3.java:14: non-static variable this cannot be referenced from a static cont
    ext
                    return this.balance;
      

  8.   

    根据你的要求,可以改成两种:
    1、按照99%的人的标准想法来改,要调用一个类的一个方法,先要创造一个类的实例,所以需要加上Myapp3 shili = new Myapp3();这句,然后调用shili这个实例的getB()方法。
    public class Myapp3 {
    int balance = 9; public static void main(String[] args) {
    Myapp3 myapp3 = new Myapp3();
    myapp3.getB();
    } int getB() {
    return this.balance;
    }
    }
    2、非要保持调用静态方法getB(),而静态方法不允许使用this,所以去掉this,会发现一个静态方法不能访问一个非静态的域balance,所以还要把balance变成静态的。最后如下
    public class Myapp3 {
    static int balance = 9; public static void main(String[] args) {
    getB();
    } static int getB() {
    return balance;
    }
    }
      

  9.   

    to:magic_lu2008(魔笛) ( ) 信誉:100 你的代码的 main 里,哪里调用了非静态方法了?没有啊?
      

  10.   

    我指的是这段代码:import java.io.*;
    public class myapp3 
    {
    int balance =9;public static void main(String[] args) 
    {
    num2 num = new num2();
    num.print();
    }
    int getB()
    {
    return this.balance;
    }
    }class num2
    {
    num2()
    {
    System.out.println("执行构造函数");
    }
    void print()
    {
    System.out.println("*");
    }
    }
      

  11.   

    楼主的学习方式不对呀,你java入门教材大概还没看两页就跑这来发问,问什么问啊。
    自学能力也忒弱了吧。
      

  12.   

    呵呵,是基础差了点to: magic_lu2008(魔笛) ( ) 信誉:100  2006-7-12 18:45:06  得分: 0  
       
    三楼的兄弟,不一定“必须是static 才能在 static  void main()中调用”,以下的调用也可以阿:
    import java.io.*;
    public class myapp3 
    {
       int balance =9;   public static void main(String[] args) 
       { 
          num2 num = new num2();
          num.print();
       }
       int getB()
       {
         return this.balance;
       }
    }class num2
    {
       num2()
       {
          System.out.println("执行构造函数");
       }
       void print()
       {
          System.out.println("*");
       }
    }
    ================================================
     num2 num = new num2();
     num.print();
    你这里是通过对象num 调用的print();方法,当然可以。
     
    public static void main(String[] args) {
       getB();
    }
    但是你这里的写发相当于
    public static void main(String[] args) {
       this.getB();
    }
    而这里的this相当于类本身,而不是这个类的实例对象,所以不能调用。因为静态资源是属于类本身所有,它是在类被加载进内存的时候就已经创建的资源,它永远只有一份,也就是属于类本身的那一份资源;而非静态资源是在类被实例化的时候才创建的资源,它是属于类的实例对象,所以类有多少个实例对象,就有多少份非静态资源。所以静态资源可以被类本身,或是类的实例对象访问。但是非静态资源(如这里的getB()方法)是只能通过类的实例对象来访问的。