0

Possible Duplicate:
Cannot make a static reference to the non-static method

Java was my first programming class. We learned the basics of OOP, but I never had this come up in class. In my next programming language, C# we used visual studio and this problem never came up either. How do you get out of the main method to access other classes? I looked back at my java programs that I wrote in class, and it looks like all method were static. In c# I made many programs without using a single static method. Can someone show me how to make this work? I'm trying to rewrite a program in java that I wrote in C# but I can't seem to figure out how to get out of the main. Here's the first class with the main method:

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class DeluxKenoMainWindow extends JFrame 
{
    private Numbers gameNumbers;

   public DeluxKenoMainWindow()
   {
    initUI();   
   }

   public final void initUI()
   {
     setLayout(null);



    int xCoord = 85;
     int yCoord = 84;
     Button[] button = new Button[80];
     for(int i = 0; i<80; i++)
     {
         String buttonName = "button" + i;
        if(i % 10 == 0)
        {
            xCoord = 12;
            yCoord +=44;
        }


        if(i % 40 == 0)
            yCoord += 10;


         button[i] = new Button(buttonName, xCoord, yCoord, i+1);
         xCoord += 42;
         getContentPane().add(button[i]);
     }

     getContentPane().add(new Game(gameNumbers));
     getContentPane().add(new AnimatedGraphics());
     getContentPane().add(new BackgroundImage());

     setTitle("Delux Keno");
     setSize(600,600);

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setLocationRelativeTo(null);

   }

   public void startGame()
   {
       do
       {
           Boolean[] pickedNumbers = gameNumbers.getNumbers();
       }while (gameNumbers.numbersSet = false);
   }


   public static void main(String[] args)
   {
     startGame();
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            System.setProperty("DEBUG_UI", "true");
            DeluxKenoMainWindow ex = new DeluxKenoMainWindow();
            ex.setVisible(true);

        }
    });



   }


   }

and here's the class that I'm trying to access:

import java.util.Random;


public class Numbers<syncronized> {

    private volatile Boolean[] computerNumbers = new Boolean[80];


    private static final Object OBJ_LOCK = new Object();

    public volatile Boolean numbersSet;


    public void setNumbers()
    {

        synchronized(OBJ_LOCK)
        {
            int i = 0;
            Random randy = new Random(System.currentTimeMillis());

            do{
                int testNum = randy.nextInt(80);

                if(computerNumbers[testNum] = false)
                {
                    computerNumbers[testNum] = true;
                    i++;
                }

            }while(i < 20);

            numbersSet = true;
        }
    }

    public Boolean[] getNumbers()
    {
        synchronized(OBJ_LOCK)      
        {
            Boolean[] returnComputerNumbers = new Boolean[80];
            for(int i = 0; i < computerNumbers.length; i++)
            {
                returnComputerNumbers[i] = computerNumbers[i];
                computerNumbers[i] = false;
            }
            return returnComputerNumbers;

        }
    }

}

I'm sure there's a simple fix for this, but I can't seem to find it. I did look on stackoverflow and google for an answer, but none of those made sense to me. Thanks for any help!!

Community
  • 1
  • 1
Craig Smith
  • 583
  • 3
  • 9
  • 26

2 Answers2

1

Since main method is static, whenever it tries to access a member variable or method from any other class (included the class that contains the method main itself) he needs a qualified instance of the object or the obect to be static itself, you can solve it by declaring

private static Numbers gameNumbers;

so that gameNumbers won't be an instance variable for every instance of DeluxKenoMainWindow but just one instance which can be accessed from a static context.

Actually there are many solutions for your problem (which should become static and which not) so it mainly depends on your needs, just follow the principle that requires that everything accessed from a static context to be static or attached to a specific instance of an object.

Jack
  • 131,802
  • 30
  • 241
  • 343
0

You should create an instance of the class DeluxKenoMainWindow to use its instance methods. Use:

new DeluxKenoMainWindow().startGame();

or

DeluxKenoMainWindow d = new DeluxKenoMainWindow()
d.startGame();

instead!

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • but wouldn't that create a new window? – Craig Smith Nov 22 '12 at 00:35
  • Replace `DeluxKenoMainWindow ex = new DeluxKenoMainWindow();` with the code above. However, consider using `SwingWorker` to avoid freezing the GUI with infinite loop on the main thread. – Eng.Fouad Nov 22 '12 at 00:38
  • thanks, I already knew that I would need another thread to keep the gui alive, but I couldn't figure out how to get out of the main – Craig Smith Nov 22 '12 at 00:39