0

Possible Duplicate:
What is the reason behind “non-static method cannot be referenced from a static context”?

public void Sort(){
*some code*
}
public void displayResults()
  {*more code*
}

public static void main(String[] args)
{
Sort();
displayResults();
}

Why am I getting this error? I have sort(); in another abstract class and then this class here is extending it.

-Confused

Community
  • 1
  • 1
jackie
  • 624
  • 2
  • 13
  • 35

2 Answers2

2

You need to instantiate the class that contains Sort(), displayResults() and main before you can call Sort() or displayResults() from main().

class Example {
    public void Sort(){
        // *some code*
    }
    public void displayResults()
    {
        // *more code*
    }
    public static void main(String[] args)
    {
        Example ex = new Example()
        ex.Sort();
        ex.displayResults();
    }
}
ObscureRobot
  • 7,306
  • 2
  • 27
  • 36
0

You need an instance of a class to call a non-static method. Calling from a static method, you don't have an instance, as statics are associated to a class, not to an instance. Therefore, you are not allowed to call non-static methods or access non-static variables from inside a static context.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625