0

Specifically using Java. What makes a function static? what does that mean? How should you choose to make the function static or not?

My while loop is broken because "non-static method hasPrecedence(java.lang.String,java.lang.String) cannot be referenced from a static context"

  • 1
    This may help answer your question: http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods – Mike Koch Feb 03 '14 at 01:18
  • 1
    There are a few other links in the related sidebar that may help you. – Julio Feb 03 '14 at 01:19
  • It means that the `static` keyword was specified on the method definition. The `main` method, eg, must always be `static`. – Hot Licks Feb 03 '14 at 01:35

2 Answers2

2

A static method is called outside of a specific instance of a class. For example, in the Java library, the Math.sqrt() function is not associated with a particular object.

In contrast, non-static methods must be called with a specific object. For example, a toUpperCase() method is defined for String objects:

String str = "Hello World.";
String upperStr = str.toUpperCase();
System.out.println(upperStr);

Notice that instead of calling String.toUpperCase() I used str.toUpperCase().


A static method is called outside of any specific object. A non-static method is called with a specific object to operate on.

therealrootuser
  • 10,215
  • 7
  • 31
  • 46
0

A function is static if there is only one shared among all instances of a class. Generally, a static function is used for "utility" work. By this I mean, the function does some work for you that does not/should not require making an instance of the class. A static function therefore does not make use of or have access to instance variables or instance methods.

M7Jacks
  • 124
  • 9