-2
CollegeTester.java:10: error: non-static method getCommand() cannot be referenced from a static context
        getCommand();//goes to command
    ^

How would I enter this method. Making it public static void only causes more problems

import java.util.Scanner;

public class CollegeTester
{   
    public String name;
    Scanner input = new Scanner(System.in);

    public static void main(String[] args)
    {
        getCommand();//goes to command
    }

    //Ask user for a command
    public void getCommand()
    {
        // do stuff
    }
}
Shimy
  • 53
  • 10
  • 3
    You should really learn a bit more about OOP (here in particular the difference between static and instance methods) before asking any more questions on this site. – Njol Jan 29 '14 at 08:45
  • Besides Njol's advice, i should also separate the CollegeTester class from the main class. – KarelG Jan 29 '14 at 08:47
  • possible duplicate of [Non-static method (method name()) cannot be referenced from a static context. Why?](http://stackoverflow.com/questions/11282093/non-static-method-method-name-cannot-be-referenced-from-a-static-context-wh) –  Jan 29 '14 at 08:48

5 Answers5

2

You can call it as in main :

CollegeTester c = new CollegeTester();
c.getCommand();
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
G.S
  • 10,413
  • 7
  • 36
  • 52
0

getCommand() is not static so you can not call this in main() as main() is static.You have create an object and then call getCommand().

Or make getCommand() static

This is the way by creating object and calling getCommand()

    import java.util.Scanner;

    public class CollegeTester
    {   
    public String name;
    Scanner input = new Scanner(System.in);

    public static void main(String[] args)
    {
CollegeTester c=new CollegeTester();
        c.getCommand();//goes to command
    }

    //Ask user for a command
    public void getCommand()
    {
        do stuff
    }

This is the way of making getCommand() static

import java.util.Scanner;

public class CollegeTester
{   
public String name;
Scanner input = new Scanner(System.in);

public static void main(String[] args)
{
    getCommand();//goes to command
}

//Ask user for a command
public static void getCommand()
{
    do stuff
}
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0

Non-static methods are instance methods, and thus accessed over an instance of the class:

new CollegeTest().getCommand();
Peter Walser
  • 15,208
  • 4
  • 51
  • 78
0

Create an instance of the CollegeTester class in main method and call the method.

new CollegeTester().getCommand();
gowtham
  • 977
  • 7
  • 15
0

You need to create an instance of CollegeTester :

   main(...)
   {
      CollegeTester t = new CollegeTester();
      t.getCommand();
   }
jacquard
  • 1,307
  • 1
  • 11
  • 16