Static method can call another static method simply by its name within same class.

Static method can call another non staic method of same class only after creating instance of the class.

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.

I think it will be more helpful if you provide the example. Provide a simple class declaration with a method or two. Include the method call you are asking about. Then we can say "yes that is allowed" or "not it is not, here is the reason".
–
John WattsJul 9 '12 at 21:09

Sure you can do it, just write a simple class: 2 static methods, 2 non-static methods and try to achieve what you are asking and the answer will be clear enough for you.
–
BenjaminJul 9 '12 at 21:15

5 Answers
5

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

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

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