What's the difference between a static and non-static method?
I always used static without knowing exactly why.
Re: What's the difference between a static and non-static method?
Did you try to do a web search? This is the first hit for me, and probably a resource for many of your questions now or in the future
Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Re: What's the difference between a static and non-static method?
If you want a particular method of a class (w.r.t its access specifiers) to be called, without creating an object of that class we usually make it as static.
But on the other hand if you want a method of a class to be called only when its instance/object is created then we make it non static.[i.e we don't suffix it with the keyword static,which in turn by default become non static method].
Basic example is your public static void main(string args[]){} (start point method)of any application.
Re: What's the difference between a static and non-static method?
Quote:
Originally Posted by
rohan22
If you want a particular method of a class (w.r.t its access specifiers) to be called, without creating an object of that class we usually make it as static.
But on the other hand if you want a method of a class to be called only when its instance/object is created then we make it non static.[i.e we don't suffix it with the keyword static,which in turn by default become non static method].
Basic example is your public static void main(string args[]){} (start point method)of any application.
I too have learned to make the compiler happy with out truly understanding what static means.
So does this make sense then?
Code :
public class Creator {
//visual of default constructor
public Creator(){}
//empty methods 1st non static, 2nd static.
public void nonStaticMethod(){}
public static void staticMethod(){}
//main method
public static void main(String[] args){
Creator methodUser = new Creator();
methodUser.nonStaticMethod();
methodUser.staticMethod();
//Creator.nonStaticMethod(); // ***doesnt compile***
Creator.staticMethod();
}
}
So to be clear, can someone explain these 4 situations separately?
1)methodUser.nonStaticMethod();
2)methodUser.staticMethod();
3)Creator.nonStaticMethod(); // ***doesnt compile***
4)Creator.staticMethod();
Feel free to add any deeper understanding to fully reveal what static means.
Thanks!
Jonathan.
Re: What's the difference between a static and non-static method?
Quote:
So to be clear, can someone explain these 4 situations separately?
1)methodUser.nonStaticMethod();
2)methodUser.staticMethod();
3)Creator.nonStaticMethod(); // ***doesnt compile***
4)Creator.staticMethod();
As i said here is certain changes to your queries
1)methodUser.nonStaticMethod();.............. works fine.
2)methodUser.staticMethod();...............you can simply call as staticMethod();....its of the same class.
3)Creator.nonStaticMethod(); // ***doesnt compile***...........will not compile because you are calling non static method
using its class name and not using the class's instance/object...i.e methodUser. To call a non static method of any class you
need to create an instance/object of that class.
4)Creator.staticMethod();..........call it simply as staticMethod(); since its of the same class.But yes if its of different class say
Creator2{
staticMethod();
}
then you can call it from Creator class as Creator2.staticMethod(); (Assuming these classes are in the same package or imported)