I always used static without knowing exactly why.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I always used static without knowing exactly why.
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)
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?
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.
As i said here is certain changes to your queriesSo to be clear, can someone explain these 4 situations separately?
1)methodUser.nonStaticMethod();
2)methodUser.staticMethod();
3)Creator.nonStaticMethod(); // ***doesnt compile***
4)Creator.staticMethod();
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)