+ 1

What does the word static mean in the part of the hello world program which says "public static void" ? It states the following about it which i put in the description but i dont quite understand what it means. Can someone explain it better?

When it starts explaining what some of the things mean in the hello world program it says the following for the word static which i dont understand! "Static:Method can be run without containing an instance of the class of the main method."

23rd Aug 2016, 3:35 AM
Richard Kainer
Richard Kainer - avatar
1 Respuesta
+ 3
You'll understand it better when you get into classes, but I'll try and explain. When you create a seperate class with methods, you have to create an object of that class in order to use those methods. If the method is static, however, you don't have to create an object. Example: class Dragon{ void roar(){ System.out.println("Rawr!"); } } class Program{ public static void main(String[] args){ Dragon drake = new Dragon(); drake.roar(); } } In this example, I created a dragon class, then instantiated it in the main class. The object I created was drake, which is the name for the Dragon object. I call its method with the objects name, a period, followed by method name. If roar was static void roar(); I could delete the Dragon drake = new Dragon(); line and replace the drake.roar(); line with just Dragon.roar(); No object is created. The reason main is static is so the compiler doesnt have to create an object just to call the main method.
23rd Aug 2016, 6:57 AM
James
James - avatar