I want to understand what does the word ‘static’ do in the ‘writeNumbers’ method header?:
public class DisplayClass {
/**
* @param args
*/
public static void main(String[] args) {
writeNumbers();
}
public static void writeNumbers()
{
int count;
for(count=1; count<=20; count++)
{
System.out.println(count);
}
}
}
The term
staticmeans that the method is available at the Class level, and so does not require that an object is instantiated before it’s called.Because
writeNumberswas being called from a method that was itselfstaticit can only call other static methods, unless it first instantiates a new object ofDisplayClassusing something like:only once this object has been instantiated could non-static methods been called, eg: