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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11403156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:57:11  来源:igfitidea点击:

Static and Non Static Method Intercall in Java

javastaticnon-static

提问by Vishal

I am clearing my concepts on Java. My knowledge about Java is on far begineer side, so kindly bear with me.

我正在清除我对 Java 的概念。我对 Java 的了解还远远不够,所以请多多包涵。

I am trying to understand static method and non static method intercalls. I know --

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

  1. Static method can call another static method simply by its name within same class.
  2. Static method can call another non staic method of same class only after creating instance of the class.
  3. Non static method can call another static method of same class simply by way of classname.methodname - No sure if this correct ?
  1. 静态方法可以在同一个类中简单地通过其名称调用另一个静态方法。
  2. 静态方法只有在创建类的实例后才能调用同一个类的另一个非静态方法。
  3. 非静态方法可以简单地通过 classname.methodname 调用同一类的另一个静态方法 - 不确定这是否正确?

My Question is about non static method call to another non staic method of same class. In class declaration, when we declare all methods, can we call another non static method of same class from a non static class ?
Please explain with example. Thank you.

我的问题是关于对同一类的另一个非静态方法的非静态方法调用。在类声明中,当我们声明所有方法时,是否可以从非静态类中调用同一类的另一个非静态方法?
请举例说明。谢谢你。

回答by Talon876

Your #3, is correct, you can call static methods from non-static methods by using classname.methodname.

您的 #3 是正确的,您可以使用 classname.methodname 从非静态方法调用静态方法。

And your question seems to be asking if you can call non-static methods in a class from other non-static methods, which is also possible (and also the most commonly seen).

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

For example:

例如:

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");
}
}

回答by user unknown

A method is and should be in the first regard be semantically bound to either the class or the instance.

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

A List of something has a length or size, so you ask for the size of special List. You need an object of that class to call .size ().

某事物的 List 具有长度或大小,因此您要求特殊 List 的大小。您需要该类的一个对象来调用.size ().

A typical, well known example of a static method is Integer.parseInt ("123");. You don't have an Integer instance in that moment, but want to create one.

静态方法的一个典型的、众所周知的例子是Integer.parseInt ("123");. 在那一刻你没有一个 Integer 实例,但想创建一个。

If, at all, we would bind that method to an instance, we would bind it to a String instance - that would make sense:

如果我们将那个方法绑定到一个实例,我们会将它绑定到一个 String 实例——这是有道理的:

int a = "123".parseInt ();

That would have been a reasonable choice, but it would mean that similar methods for float, double, long, short, Boolean and maybe every class which has a symmetric "toString" method would have to be put into String. That would have meant a zillion of extensions to the String class.

这本来是一个合理的选择,但这意味着类似的 float、double、long、short、Boolean 方法以及每个具有对称“toString”方法的类都必须放入 String 中。那将意味着对 String 类的无数扩展。

Instead String is final, so a reasonable place to put such a method is the target class, like Integer, Float and so on.

String 是 final 的,所以放置这种方法的合理位置是目标类,如 Integer、Float 等。

回答by Pshemo

You can call non-static method from non-static method using explicitly reference to object on which you want to call that method someObject.method, or without specifying that object someMethod()(in this case it will be invoked on same object that you are invoking current non-static method).

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

Maybe this will show it better

也许这会更好地展示它

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);
    }
}

output:

输出:

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

回答by mindandmedia

not sure that I understand the question correctly, but non-static methods are the standard way to design classes in OO. Maybe this sample will help spark the discussion:

不确定我是否正确理解了这个问题,但非静态方法是在 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(); 
   }
}

回答by DrLudwig3

public class TestClass{

  public static void testStatic(){
   System.out.println("test1");
  }

  public void testNonStatic(){
    System.out.println("test2");
  }

  public void test1(){
    // both is valid
    testStatic();  
    TestClass.testStatic();

    // this is valid, cause it can call the method of the same instance of that class
    testNonStatic();   
    this.testNonStatic();

    // this is not valid, cause without a concrete instance of a class you cannot call
    // non static methods
    TestClass.testNonStatic();
  }

  public static void test2(){
    // again these are both correct
    testStatic();  
    TestClass.testStatic();

    // this doesn't work, cause you cannot call non static methods out of static methods
    testNonStatic();   
    this.testNonStatic();

    // this instead does work cause you have a concrete instance of the object
    TestClass myTestClass = new TestClass();
    myTestClass.testNonStatic();

    // this still doesn't work
    TestClass.testNonStatic();
  }

}