-1

I have an array:

private String[] gamesArray = new String[] {"spin", "tof"};

And when I start the program, I want the program to print the items in the array:

Please select a game: spin, tof.

This is my attempt:

import java.util.Scanner;

public class Main {

private Scanner console = new Scanner(System.in);
private Spin spin = new Spin();
private String input = "";
private String[] gamesArray = new String[] {"spin", "tof"};

public static void main (String[] args) {

    System.out.println("Welcome to the system!");

    for (String s : gamesArray) {

        System.out.println("Please select a game:" + s);

    }

}
}

Error:

src\Main.java:15: error: non-static variable gamesArray cannot be referenced fro
m a static context
                for (String s : gamesArray) {
                                ^

What is wrong?

Jony Kale
  • 979
  • 3
  • 15
  • 35
  • Rather than making gamesArray a field, you could create the variable inside of your main method – mistahenry Jun 24 '13 at 19:24
  • On a side note, this will not print what you intend. Rather it will print: Please select a game: spin Please select a game: tof – ardent Jun 24 '13 at 19:32

3 Answers3

3

You're trying to print an instance field without an instance. The main() method is static.

You could solve this by declaring gamesArray to be a static field. For example:

 private static final String[] gamesArray = new String[] {"spin", "tof"};

Alternatively, you could make an instance of the class Main(), and accessing the field through that.

 Main myMain = new Main();
 for (String s : myMain.gamesArray) {

Once you do one of those, you'll notice the output looks like this:

Please select a game:spin
Please select a game:tof

You may want the "Please select the game" to be outside the loop.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • Thanks Andy, can you please link me to a place that explains everything about the difference between a regular int, String, other methods, and Statics + void methods? I've been searching the docs, couldn't really find what I want. – Jony Kale Jun 24 '13 at 19:25
  • @JonyKale, http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html is a good place to start. – ardent Jun 24 '13 at 19:26
  • @JonyKale - Here's another: http://stackoverflow.com/questions/797964/what-is-the-exact-meaning-of-static-fields-in-java . As an example, consider a hypothetical class Human. The species name could be a static field. An individual's height could be an instance field. – Andy Thomas Jun 24 '13 at 19:30
1

You need to create the object of class first

 Main obj = new Main();
 for(String s : obj.gamesArray){
       System.out.println("Please select a game:" + s);

 } 
  • A static method or variable can be accessed directly from static method. If you declare the array as private static String[] gamesArray = new String[] {"spin", "tof"}; then you could have accessed it without creating the object of Main class
  • But you can access a static variable directly from an instance method because static variables are shared variables across all the instances.
  • If you want to access instance variables directly, then you can access them in instance methods and not static methods.

    public class StaticNonStatic {
    
    private static int staticVar;
    private int instanceVar;
    
    public void instanceMethod(){
        System.out.println(instanceVar);// instance variable accessed directly
        System.out.println(staticVar); // static variable accessed directly from instance method
    }
    
    public static staticMethod(){
        System.out.println(instnaceVar); // no this is wrong, can't access instance member from static block directly
        System.out.println(instnaceVar); // fair enough, accessing static member from static method directly
    }
    
    }
    
Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
0

GamesArray is an instance member of your class. Since main method is always static there is no instance of the class to get at gamesArray. Consider making gamesArray static

aaronman
  • 18,343
  • 7
  • 63
  • 78