Java中的静态和非静态方法调用

Java中的静态和非静态方法调用,java,static,non-static,Java,Static,Non Static,我正在澄清关于Java的概念。我对Java的了解远远不够,所以请耐心等待 我试图理解静态方法和非静态方法调用。我知道-- 静态方法可以在同一个类中通过名称调用另一个静态方法 静态方法只能在创建类的实例后调用同一类的另一个非staic方法 非静态方法可以通过classname.methodname调用同一类的另一个静态方法-不确定这是否正确 我的问题是关于对同一类的另一个非staic方法的非静态方法调用。在类声明中,当我们声明所有方法时,是否可以从一个非静态类调用同一类的另一个非静态方法? 请举例

我正在澄清关于Java的概念。我对Java的了解远远不够,所以请耐心等待

我试图理解静态方法和非静态方法调用。我知道--

  • 静态方法可以在同一个类中通过名称调用另一个静态方法
  • 静态方法只能在创建类的实例后调用同一类的另一个非staic方法
  • 非静态方法可以通过classname.methodname调用同一类的另一个静态方法-不确定这是否正确
  • 我的问题是关于对同一类的另一个非staic方法的非静态方法调用。在类声明中,当我们声明所有方法时,是否可以从一个非静态类调用同一类的另一个非静态方法?

    请举例说明。谢谢。

    我不确定我是否正确理解了这个问题,但非静态方法是在OO中设计类的标准方法。也许此示例将有助于引发讨论:

    public class MySampleClass{
    
       private void methodA(){
           System.out.println('a called');
       }
    
       public void methodB(){
           this.methodA();
           staticMethod(this);
       }
    
       private static void staticMethod( MySampleClass inst ){
           inst.methodA(); 
       }
    }
    
    您的#3是正确的,您可以使用classname.methodname从非静态方法调用静态方法

    您的问题似乎是问您是否可以从其他非静态方法调用类中的非静态方法,这也是可能的(也是最常见的)

    例如:

    public class Foo {
    
    public Foo() {
        firstMethod();
        Foo.staticMethod(); //this is valid
        staticMethod(); //this is also valid, you don't need to declare the class name because it's already in this class. If you were calling "staticMethod" from another class, you would have to prefix it with this class name, Foo
    }
    
    public void firstMethod() {
        System.out.println("This is a non-static method being called from the constructor.");
        secondMethod();
    }
    
    public void secondMethod() {
        System.out.println("This is another non-static method being called from a non-static method");
    
    }
    
    public static void staticMethod() {
        System.out.println("This is the static method, staticMethod");
    }
    }
    

    方法首先是并且应该是语义上绑定到类或实例的

    某事物的列表有一个长度或大小,所以你需要特殊列表的大小。您需要该类的对象来调用
    .size()

    一个典型的、众所周知的静态方法示例是
    Integer.parseInt(“123”)。此时您没有整数实例,但希望创建一个

    如果我们将该方法绑定到一个实例,我们将它绑定到一个字符串实例,这是有意义的:

    int a = "123".parseInt ();
    
    这将是一个合理的选择,但这将意味着类似的float、double、long、short、Boolean方法,以及可能每一个具有对称“toString”方法的类都必须放入字符串中。这就意味着对String类进行了大量的扩展


    相反,字符串是最终的,所以放置这种方法的合理位置是目标类,如Integer、Float等

    您可以使用对要调用该方法的对象的显式引用从非静态方法调用非静态方法
    someObject.method
    ,或者不指定该对象
    someMethod()
    (在这种情况下,它将在调用当前非静态方法的同一对象上调用)

    也许这会更好地表现出来

    class Demo {
        private String name;
    
        public Demo(String n) {
            name = n;
        }
    
        public String getName() {// non static method
            return name;
        }
    
        public void test(Demo d) {// non-static method
            System.out.println("this object name is: "+getName());// invoking method on this object
            System.out.println("some other object name is: "+d.getName());// invoking method on some other object
        }
        //test
        public static void main(String[] args) {
            Demo d=new Demo("A");
            Demo d2=new Demo("B");
            d.test(d2);
        }
    }
    
    输出:

    this object name is: A
    some other object name is: B
    

    您是否编写了一个测试用例并进行了尝试?我认为如果您提供一个示例,将会更有帮助。用一两个方法提供一个简单的类声明。包括您询问的方法调用。然后我们可以说“是的,这是允许的”或“不是,这是原因”。当然你能做到,只需编写一个简单的类:2个静态方法,2个非静态方法,并尝试实现你所要求的,答案将对你足够清楚。这是对我最好的解释,消除了所有疑问!!谢谢你,朋友
    this object name is: A
    some other object name is: B