Just put the keyword "static" in front of your method.
static means the method will not be instintated. In English that means the variables and methods with the static modifyer load first into memory. Just about all the methods of Math are static thus we can use Math.PI, Math.Random().
If a class is not made into an Object with the keyword 'new' then you MUST use static. Many single-class programs with just a main() do not instiantate the class. main() is static for several reasons.
class StaticExample {
public static void main(String [] args) {
String s = "This is a happy string";
}
}
static is not needed because it is inside the main()
class StaticExample1 {
static String s = "This is a lessor string";
public static void main(String[] args) {
System.out.println( s );
}
}
class StaticExample2 {
pubic static void main(String[] args) {
String s = "Happy string";
System.out.println("The length of " + s +" is " + getLen(s) );
}
static private int getLen(String sentence) {
return sentence.length();
}
}
// and finally, as an Object
class StaticExample3 {
String s;
public StaticExample3() {
s = "The last of the happy strings";
showLength();
public static void main( String[] args ) {
new StaticExample3();
}
private void showLength() {
System.out.println("The length of + s + " is " + s.length() );
}
}
the last example makes an Object and that copies all the vars and methods into memory ... except main(); Because we have an Object we have methods, states, and values we can use with dot notation. If we don't have an Object we must use the code as it is read in, thus static. Finally, static is used to indicate a CONSTANT and by coder convention the label-name for a constant is CAPS. ex. static double TRUNICATED_PI = 3.1457;