简体   繁体   中英

Static And Non Static Method Intercall In Java

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

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 ?

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.

Your #3, is correct, you can call static methods from non-static methods by using 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");
}
}

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 () .

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.

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

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.

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

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:

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

}

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM