This common error occurs when you attempt to call an instance method from a static method, most commonly main(). For example,

class StaticTest {
  public static void main(String[] args) {
    int i = getX();
  public int getX() {
    return 3;  

The static method, main(), belongs to the class. However, getX() belongs to an object in the class. The compiler doesn't know on which object it's invoking the getX() method.

There are a couple of ways around this problem. You could declare that getX() is also static; that is:

  public static int getX() {

Alternately, you can instantiate an object in the StaticTest class in the main() method and invoke that object's getX() method, like this:

  public static void main(String[] args) {
    StaticTest st = new StaticTest();
    int i = st.getX();

Posted in: Java

Related FAQ's

Marius Ion ANGEL HOT SOFT LLC (800) 316-7677