Call non static method from static method javascript
I am curious as I get a "undefined is not a function" error. Consider the following class: Show
Refactoring aside, the issue is on the line: I assume this is because I have a static method who's internals call a non static method? Would that non
static method have to be static? and if so does the concept of asked Oct 27, 2015 at 15:17
2 No, a static method cannot call a non-static method. Consider that you have objects Regardless, you can get around this by passing answered Oct 27, 2015 at 15:23
JoshJosh 8,0093 gold badges25 silver badges48 bronze badges 0 It makes no sense to call a non-static function from a static one, in any language.
Static (in this context) means that it's basically outside of the object, independent in all but name. It's not tied to any instance and thus there's no answered Oct 27, 2015 at 15:20
BlindyBlindy 62.2k10 gold badges86 silver badges125 bronze badges 3 No, in general static methods cannot call instance methods. It makes little sense to be able to do so. The only caveat is that there is nothing stopping a static method instantiating an instance of a class, at which point it can call the instance methods in the usual way. answered Oct 27, 2015 at 15:21
JamiecJamiec 131k13 gold badges135 silver badges189 bronze badges A static method provides NO reference to an instance of its class (it is a class method) hence, no, you cannot call a non-static method inside a static one. Of course you may either
or
Due to the vagueness of your question there can be many other ways (for instance, calling a non-static method of an object of a completely unrelated class). Providing examples is left as an exercise. Can static method call non static method Javascript?No, a static method cannot call a non-static method.
Can we call non static method from static?The only way to call a non-static method from a static method is to have an instance of the class containing the non-static method. By definition, a non-static method is one that is called ON an instance of some class, whereas a static method belongs to the class itself.
Can we call non static method from main method?Since you want to call a non-static method from main, you just need to create an object of that class consisting non-static method and then you will be able to call the method using objectname.
How do you call a non static method from a static method in Java in same class?If we are calling a non static method then we need to use object so that it will call corresponding object non static method. Non static methods will be executed or called by using object so whenever we want to call a non static method from static method we need to create an instance and call that method.
|