【问题标题】:Nonstatic method can't be called to static [duplicate]不能将非静态方法调用到静态[重复]
【发布时间】:2013-05-24 16:57:22
【问题描述】:

好的...所以我已经做了很多试验和错误...我似乎无法将我的 usFormat 方法调用到我的 main 中,我想我已经把整个事情搞砸了,哈哈。 。 你能帮忙的话,我会很高兴。只是...请在初学者级别。

 class Date {
     String date;
     String day;
     String month;
     String year;
     StringTokenizer st;
     Scanner sc = new Scanner (System.in);


     //instance vars go here
     public Date (String date){
         while (sc.hasNextLine()) {
             st = new StringTokenizer (sc.nextLine());
             this.month =  st.nextToken();
             this.day   =  st.nextToken();
             this.year  =  st.nextToken();
        }

        } //end constructor

        public String usFormat () {
            return month + " " + day + "," + year;
        } //end usFormat
        public String euFormat () {
            return null;
        } //end euFormat

        public static void main (String[] args){
            Date usFormat = new Date (date);

        }
    } 

【问题讨论】:

  • -1。请google您的错误消息,这包含在无数的 SO 问题和在线教程中。
  • 这是您完全相同的问题:stackoverflow.com/questions/7379915/…
  • @所有回答者,请关闭重复而不是重复答案。

标签: java


【解决方案1】:

由于您从 static method 调用方法,因此您还需要将该方法(usFormat()euFormat())设为静态。

public static String ...

静态基本上意味着您不需要该类的实例。所以.. 你不需要一个实例来运行main,但你确实需要一个实例来调用usFormat()(因为它不是静态的)。那是行不通的。因此出现错误。

如果您不想将这些方法设为静态,请考虑将您的代码移出主类并移至另一个类。您可以在 main 中使用 new 创建此类的实例(这也适用于给定的类,以防您也需要 (new Date()).usFormat())。

【讨论】:

    【解决方案2】:

    您不会通过将变量声明为与方法相同的名称来调用对象上的方法。这就是你调用方法(并输出它)的方式。

    Date d = new Date("a string");  // You don't seem to be using the argument anyways.
    String formattedDate = d.usFormat();
    System.out.println(formattedDate);
    

    此外,您甚至不使用 Date 构造函数的参数。把String date参数去掉就行了,调用构造函数的时候就可以去掉"a string"了。

    【讨论】:

      【解决方案3】:

      您定义了一个名为 Date 的实例类,因此您只需执行以下操作:

          public static void main (String[] args){
              Date usFormat = new Date (date);
              System.out.println(date.usFormat());
          }
      

      无需将Date 类的任何方法设为静态。

      【讨论】:

        【解决方案4】:

        另一种方法是在 main 方法中创建 Date 的实例,例如 myDate = new Date("");,然后执行 myDate.usFormat();

        【讨论】:

          【解决方案5】:

          要从 static method 调用非方法,您必须执行以下操作之一:

          1) 实例化定义了非静态方法的类。

          Date usFormat = new Date (date);
          System.out.println(date.usFormat());
          

          2) 制作非静态方法static

          public static .... 
          

          我建议你使用第一个选项,因为第二个需要更多的重构。

          【讨论】:

            【解决方案6】:

            非静态方法也称为实例方法,即它们需要类的实例。 实例或对象是您使用关键字new 创建的东西。例如,class 就像汽车,object 就像您驾驶的特定汽车。

            因此,要调用该方法,您必须执行以下操作:

              public static void main (String[] args){
                Date usFormat = new Date (date);
                String formattedDate = usFormat.euFormat();
              }
            

            【讨论】:

              【解决方案7】:

              在您的代码中,静态/非静态方法没有任何问题。只有一些错别字:

               class Date {
               String date;
               String day;
               String month;
               String year;
              
               Scanner sc = new Scanner (System.in);
              
              
               //instance vars go here
               public Date (String date){
                       StringTokenizer st= new StringTokenizer (date, " ");
                       this.month =  st.nextToken();
                       this.day   =  st.nextToken();
                       this.year  =  st.nextToken();
              
                  } //end constructor
                  public String usFormat() {
                      return month + " " + day + "," + year;
                  } //end usFormat
                  public String euFormat() {
                      return null;
                  } //end euFormat
                  public static void main (String[] args){
                    Date usFormat = new Date ("20 10 2013");    
                  }
              } 
              

              但是,关于您的问题,对于调用静态方法,您不需要类的实例。

              【讨论】:

                【解决方案8】:

                将日期定义为静态。 static String date; 你不能从静态方法 main 中引用非静态变量

                【讨论】:

                • 注意它访问的是实例变量。
                猜你喜欢
                • 2023-02-22
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2023-03-26
                • 1970-01-01
                • 2018-10-10
                • 1970-01-01
                相关资源
                最近更新 更多