【问题标题】:why can't I create another method aside from main method in main class?为什么我不能在主类中创建除主方法之外的另一个方法?
【发布时间】:2013-11-06 04:49:48
【问题描述】:

我是Java初学者,我有一个关于主类和主方法的基本问题。我尝试在主方法下创建诸如加法之类的方法。抛出“非静态方法”之类的错误。什么是理由?谢谢...

【问题讨论】:

  • 请贴出你的代码,让我们看看。

标签: java methods static main


【解决方案1】:

静态方法意味着您不需要在实例(对象)上调用该方法。非静态(实例)方法要求您在实例上调用它。所以想一想:如果我有一个方法 changeThisItemToTheColorBlue() 并且我尝试从 main 方法运行它,它会改变什么实例?它不知道。您可以在实例上运行实例方法,例如 someItem.changeThisItemToTheColorBlue()

更多信息http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods

从静态方法调用非静态方法的唯一方法是拥有一个包含非静态方法的类的实例。根据定义,非静态方法是在某个类的实例上调用的方法,而静态方法属于该类本身。

就像你在没有实例的情况下尝试调用String类的非静态方法startsWith一样:

 String.startsWith("Hello");

你需要有一个实例,然后调用非静态方法:

 String greeting = new String("Hello World");
 greeting.startsWith("Hello"); // returns true 

所以你需要创建实例来调用它。

为了更清楚地了解静态方法,您可以参考

https://softwareengineering.stackexchange.com/questions/211137/why-can-static-methods-only-use-static-data

【讨论】:

    【解决方案2】:

    我猜你使用的是这样的代码。

    public class TestClass {
    
    public static void main(String[] args) {
        doSth();
    }
    
    public void doSth() {
    
    }
    

    您不能从主类调用非静态方法。 如果你想从你的主类调用一个非静态方法,像这样实例化你的类:

    TestClass test = new TestClass();
    test.doSth();
    

    并调用方法。

    【讨论】:

      【解决方案3】:

      我认为您在没有关键字“静态”的情况下定义了您的方法。 您不能在诸如 main 方法之类的静态上下文中调用非静态方法。

      Java Object Oriented Programming

      【讨论】:

        【解决方案4】:

        main方法是静态方法,所以不存在于对象内部。

        要调用非静态方法(定义前没有“static”关键字的方法),您需要使用new创建类的对象。

        您可以将其他方法设为静态,这将解决当前的问题。但这样做可能是也可能不是好的面向对象设计。这取决于你想做什么。

        【讨论】:

          【解决方案5】:

          如果没有实例化类,就不能从静态方法调用非静态方法。如果您想在不创建主类的新实例(新对象)的情况下调用另一个方法,则还必须对另一个方法使用 static 关键字。

              package maintestjava;
          
              public class Test {
          
                // static main method - this is the entry point
                public static void main(String[] args)
                {
                    System.out.println(Test.addition(10, 10));
                }
          
                // static method - can be called without instantiation
                public static int addition(int i, int j)
                {
                  return i + j;
                }
              }
          

          如果你想调用非静态方法,你必须实例化类,这样创建一个新的实例,一个类的对象:

          package maintestjava;
          
          public class Test {
          
            // static main method - this is the entry point
            public static void main(String[] args)
            {
                Test instance = new Test();
                System.out.println(instance.addition(10, 10));
            }
          
            // public method - can be called with instantiation on a created object
            public int addition(int i, int j)
            {
              return i + j;
            }
          }
          

          查看更多: Static keyword on wikipedia Static on about.com

          【讨论】:

            猜你喜欢
            • 2013-10-05
            • 1970-01-01
            • 1970-01-01
            • 2015-02-07
            • 2015-04-22
            • 1970-01-01
            • 2016-01-08
            • 2018-08-11
            • 2015-03-05
            相关资源
            最近更新 更多