Calling Non-Static Method In Static Method In Java
Solution 1
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.
Solution 2
You could create an instance of the class you want to call the method on, e.g.
new Foo().nonStaticMethod();
Solution 3
Firstly create a class Instance and call the non-static method using that instance. e.g,
class demo {
public static void main(String args[]) {
demo d = new demo();
d.add(10,20); // to call the non-static method
}
public void add(int x ,int y) {
int a = x;
int b = y;
int c = a + b;
System.out.println("addition" + c);
}
}
Solution 4
public class StaticMethod{
public static void main(String []args)throws Exception{
methodOne();
}
public int methodOne(){
System.out.println("we are in first methodOne");
return 1;
}
}
the above code not executed because static method must have that class reference.
public class StaticMethod{
public static void main(String []args)throws Exception{
StaticMethod sm=new StaticMethod();
sm.methodOne();
}
public int methodOne(){
System.out.println("we are in first methodOne");
return 1;
}
}
This will be definitely get executed. Because here we are creating reference which nothing but "sm" by using that reference of that class which is nothing
but (StaticMethod=new Static method()
) we are calling method one (sm.methodOne()
).
I hope this will be helpful.
Solution 5
You need an instance of the class containing the non static method.
Is like when you try to invoke the non-static method startsWith
of class String
without an instance:
String.startsWith("Hello");
What you need is to have an instance and then invoke the non-static method:
String greeting = new String("Hello World");
greeting.startsWith("Hello"); // returns true
So you need to create and instance to invoke it.
me123
Updated on January 26, 2021Comments
-
me123 over 3 years
I'm getting an error when I try to call a non-static method in a static class.
Cannot make a static reference to the non-static method methodName() from the type playback
I can't make the method static as this gives me an error too.
This static method cannot hide the instance method from xInterface
Is there any way to get round calling an non-static method in another static method? (The two methods are in seperate packages and seperate classes).
-
CrandellWS over 8 yearsadding
.class
between the class and method did work thanks -
user207421 almost 7 yearsNo you can't. This is just fantasy, unless the method you want to call is defined in
java.lang.Class
. -
user207421 almost 7 yearsThe question isn't about constructors.
-
user207421 almost 7 yearsIt is possible, if you have an instance to call the non-statc method on. It could be provided as a parameter, for example, or be a static data member of the class.
-
Usman almost 7 yearsActually as per my understanding, calling non static method with instance is beyond the scope of this question.
-
AMDG almost 6 years@EJP For security purposes, and this is what I do with my project, in the case of dependency injection, it is useful to only have initialization code be instantiated for initialization, and not at the arbitrary discretion of code using my API. Thus, when an initialization cycle happens, the temporary object containing only constants and non-static methods which are dynamically initialized through dependency injection can be safely loaded and discarded as needed. The purpose of static methods is more like the purpose of "pure" functions such as in functional programming languages.
-
Noor Hossain over 3 yearsthanks, approach is clearer, help me lot ! sometimes, lower reputation guys' are helpful for this way.
-
Hermann Schwarz almost 3 yearsA very advanced answer! Thank you!