1

I am working on a school project, building a Chess-game. I have my board which creates the arrays and fill the board with pieces.

Now, I want to instantiate a new board in a game class and use the console to input player moves. To make this as simple as possible, my trouble is inputting non-static variables.

For instance:

public class Test extends ConsoleProgram{

public static double a1;

 public static void main(String[] args) {
    a1 = readLine("Insert value of a1"); 
    System.out.println(a1);
 }
} 

more info on readLine() here

As you probably see, this won't work as java is complaining cannot make static reference to the non-static method readLine()

How do I work around this? Maybe there is something really basic I just don't understand..

As always, thank you very much for your quick and insightful answers!

tore
  • 303
  • 3
  • 11
  • Why don't you just make `readLine()` static? Furthermore, it is not really possible to answer your question as you did not show the relevant code (`readLine()`). – home May 10 '13 at 11:21
  • And, if you are really testing your code, use JUnit. It will pay off. – Eric Jablow May 10 '13 at 11:24

6 Answers6

2

either define readline as static or

make an object of class and then call the method, new Test().readline();

Ankit
  • 6,554
  • 6
  • 49
  • 71
2

If you don't want to make your readLine method static, do a call on an instance:

public static void main(String[] args) {

    Test me = new Test();

    a1 = me.readLine("Insert value of a1");

    System.out.println(a1);

 }
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • Hey! My brain kinda melts trying to understand what is going on. Why is this OK? Could you explain to me like I'm five what is going on?Thank you so much! – tore May 10 '13 at 11:36
  • 1
    @tore - "Test" is the name of your class. When you define a method in that class without tagging it "static" it becomes an instance method. This means you can only call it if you have an instance of your class (ie, an "object"). `new Test()` creates an instance of your class (which would contain any non-static fields you might define). We assign that instance's "reference" to `me` (could just as well be `aardvark`, of course). Now we can use `me` to refer to that instance, and then `me.readLine` refers to the instance method. – Hot Licks May 10 '13 at 15:14
  • 1
    @tore - (These are very fundamental "object-oriented" concepts. You need to find a reference where you can learn them in an organized fashion -- this is not something that you should "pick up on the street".) – Hot Licks May 10 '13 at 15:15
1

Make readLine()` as static.Then you can access

If you make method as static then you can use that in main() method

otherwise create an object for Test class then use it

PSR
  • 39,804
  • 41
  • 111
  • 151
1

The simplest work around I've found is simply instantiate the object in the main method, and call the relevant methods from the constructor.

public static void main(String[] args)
{
     new ClassThing();
}

public ClassThing()
{
    // Make your method calls here.
}

This means you can access instance methods, and you don't need to call everything from a static context.

christopher
  • 26,815
  • 5
  • 55
  • 89
1

Create Test instance in main and call it's instance method.

public class Test extends ConsoleProgram {
    public double a1;

    public void run() {
        a1 = readLine("Insert value of a1");
        System.out.println(a1);     
    }

    public static void main(String[] args) {
        Test main = new Test();
        main.run();
    }

}
rzymek
  • 9,064
  • 2
  • 45
  • 59
  • Thank you for your input! Could you please give tell me why you use the method run()? Is it something special about that one, or could you have named it something else? – tore May 10 '13 at 11:30
  • The name `run` is arbitrary. You can pick any method name. The method can also be `private`. – rzymek May 10 '13 at 11:48
1

You should instantiate an object of the class that has the method readLine().

If that class' name is i.e. Reader you should do something like:

 Reader r= new Reader();
 a1 = r.readLine("Insert...");`
darijan
  • 9,725
  • 25
  • 38