According to the restrictions written in javatpoint, static methods cannot call non-static methods directly. What exactly is it meant by 'directly' and why cant i do that? And yeah what are the indirect ways in which i can do it?
-
1By declaring a method static, you are expressing that a call to that method does not alter the state of the object. As a result you do not need an instance of the class declaring the method but also cannot call methods that can alter the state of an instance.CannedMoose– CannedMoose2018年06月27日 07:25:17 +00:00Commented Jun 27, 2018 at 7:25
-
1"what are the indirect ways in which i can do it" depends on what you want to achieve.Federico klez Culloca– Federico klez Culloca2018年06月27日 07:26:43 +00:00Commented Jun 27, 2018 at 7:26
-
"static methods cannot call non-static methods directly" is a very misleading statement. If the tutorial really says that, that's unfortunate.T.J. Crowder– T.J. Crowder2018年06月27日 07:32:56 +00:00Commented Jun 27, 2018 at 7:32
4 Answers 4
According to the restrictions written in javatpoint, static methods cannot call non-static methods directly.
Sure they can. They just need an instance on which to call them. Here's an example of a static method directly calling a non-static method:
class Example {
public static void main(String[] args) {
Example e = new Example();
e.hiThere(); // <========== Direct call to instance method
go(e);
}
private static void go(Example e) {
e.hiThere(); // <========== Direct call to instance method
}
private void hiThere() {
System.out.println("Hi there");
}
}
What exactly is it meant by 'directly' and why cant i do that?
They probably mean "not using an instance." It's not how I would define "directly" at all. For instance, in the example above, main cannot call hiThere without having an instance to call it on. But once it has an instance (in main's case, by creating one), it can directly call hiThere on it. Also in the example, go can directly call hiThere; it receives an instance as an parameter.
And yeah what are the indirect ways in which i can do it?
They aren't indirect: You need an instance on which to call the method. But when a static method has an instance, calling the instance method is as direct as any other method call.
Comments
It means that if you have an instance method a you can't call it from static method b so
void a() {
// ...
}
static void b() {
a();
}
is not valid.
You can't to this simply because a static method is not being run inside an instance, so it wouldn't know on what instance to call the method.
Comments
"Directly" here means that you're not calling the method on an object.
For example,
public class Foo {
public static void method1() {
Foo myFoo = new Foo();
myFoo.method2(); // This is OK - we're calling method2 on an object.
method2(); // This doesn't compile - we're trying to call method2 directly.
}
public void method2() {
System.out.println("This is method2");
}
}
So it's OK for a static method to call a non-static method, when there is an object that the non-static method is being called on. But the direct call, without referring to the object, can't be done.
You can of course call one static method from another directly. You can also call one non-static method from another directly.
Comments
Static methods are bound to the class. Non-static methods are bound to the instance of the class (object).
That means you can call static methods (directly) like this:
SomeClass.someStaticMethod();
But to call non-static methods, you need to construct an object of the class first (this is what you mean by "indirectly"):
SomeClass obj = new SomeClass();
obj.someNonStaticMethod();
So, if you are in a static method, you are outside of any object-instance. That means you cannot call a non-static method. Java doesn't magically know what object you'd like to work with / execute the method on.
public static void someStaticMethod()
{
//You aren't working with any object here
}
However, if you are in a non-static method, you are inside of an object-instance. That means you can call other non-static method of the object you are working with right now. Those are all non-static method in the class (and superclasses, if they have correctly set the access modifier).
public void someNonStaticMethod()
{
//This is valid now.
this.someOtherNonStaticMethod(); //"this." is optional
}
public void someOtherNonStaticMethod()
{
//Do some stuff
}
what are the indirect ways in which i can do it?
The "indirect" way to call a non-static method from a static one, is to first construct an object of the class with the non-static method (as shown in the second code-block).