Static And Non Static Method Intercall In Java

17,922

Solution 1

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

Solution 2

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.

Share:
17,922
Vishal
Author by

Vishal

Updated on June 04, 2022

Comments

  • Vishal
    Vishal almost 2 years

    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.

  • vidur punj
    vidur punj almost 11 years
    This is the best explanation for me cleared all doubts !! thanks pal