Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By stchman

Thread: Cannot make a static reference...

  1. #1
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default Cannot make a static reference...

    Hello. Sorry if this is very obvious, newby here...

    I want to access a method called checkAvailablity that is in a class called Play from my main class Runner. I'm calling it from inside a ActionListener event by saying Play.checkAvailability(); This gives me the error: Cannot make a static reference to the non-static method checkAvailability() from the type Play.
    Do I have to make that method static or is there a way around this?
    I also tried making new instances of the Play class but couldn't get anything to work. (When I made a new instance in my main it said it couldn't be resolved, and I got a stack overflow error when I made it outside the main).
    Thanks.

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Can you provide us with the code giving you the errors? That and the full error message you get? I'm a little tired so following your post is a bit hard for me right now.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Usually, the way to get around this is to take as much code as possible out of your main() method:

    Java Code:
    public class Foo {
        private void run() {
            // This is where the rest of your code goes
        }
        public static void main(String[] args) {
            Foo f = new Foo();
            f.run();
        }
    }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Don't make the method static.

    You need to use an instance of the Play class - but it should not be a new instance. It should be the instance (already existing) whose checkAvailability() you want to find out about.

    I agree with Dark: post code and compiler message.

  5. #5
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default

    Here is a very simple mock code I made to try explain better:

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Play extends JFrame implements ActionListener {
    	public Play() {
    		super("");
    		setSize(500, 400);
    		setVisible(true);
    	}
    	public void actionPerformed(ActionEvent buttonListener) {
    		Other.Method(); // Error here: Cannot make a static reference to the non-static method Method() from the type Other. (I don't want to make that method static)
    	}
    	public static void main(String[] args) {
    		Play p = new Play();		
    	}
    }
    Other class:

    Java Code:
    public class Other {
    	public void Method() {
    		int x = 5;
    	}
    }

  6. #6
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    As pbrockway says, you need an instance of Other to be able to call Method() on it.

    Java Code:
    Other o = new Other();
    o.Method();
    Also, method names shouldn't start with a capital letter - the compiler doesn't care, but it's good practice to follow conventions.

  7. #7
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default

    When I make a new instance of Other in the main and the call it in the actionListener it has the error: o cannot be resolved. And if I don't put it in my main then I get a stackoverflow error. (this is only with my proper program, it works fine with the small code I posted which is whats really confusing me.) Should I post my whole code here. Its pretty long though and I don't know which part is causing the problem.

  8. #8
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default

    Here is the code:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Runner extends JFrame implements ActionListener {
    
    	int squareChosen; // Square chosen by player - 0 to 8
    	char[] piecePositions = {'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u'}; // Shows which pieces are occupying which spaces.
    	Play p = new Play(); //this is causing the problem
    	
    	JPanel gameBoardPane = new JPanel();
    		JButton b0 = new JButton(new ImageIcon("board.png"));
    
    		
    	ImageIcon xIcon = new ImageIcon("boardX.png");
    	ImageIcon oIcon = new ImageIcon("boardO.png");
    	
    	JPanel mainButtonPane = new JPanel();
    		JButton newGameButton = new JButton(new ImageIcon("newGameIcon.png"));
    		JButton optionsButton = new JButton(new ImageIcon("optionsIcon.png"));
    		JButton statsButton = new JButton(new ImageIcon("statsIcon.png"));
    
    		
    		
    	public Runner() {
    		super("Naughts and Crosses");
    		setSize(900, 800);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setResizable(false);
    		gameBoardPane.setLayout(new GridBagLayout());
    		GridBagConstraints bGBC = new GridBagConstraints();
    		
    		mainButtonPane.setLayout(new GridBagLayout());
    		GridBagConstraints mGBC = new GridBagConstraints();
    		
    		
    		newGameButton.addActionListener(this);
    		optionsButton.addActionListener(this);
    		statsButton.addActionListener(this);
    		
    		b0.addActionListener(this);
    
    		// Lots of unimportant code here
    		
    		
    		// Add Items to Frame:
    		add(gameBoardPane, BorderLayout.NORTH);
    		add(mainButtonPane, BorderLayout.SOUTH);
    		
    		
    		setVisible(true);
    	}
    
    	
    	public void actionPerformed(ActionEvent buttonListener) {
    		Object source = buttonListener.getSource();
    		
    		if (source == newGameButton) {
    			System.out.println("Start new game");
    			
    		}
    		else if (source == optionsButton) {
    			System.out.println("Open options window");
    		}
    		else if (source == statsButton) {
    			System.out.println("Open stats window");
    		}
    		else if (source == b0) {
    			squareChosen = 0;
    			p.checkAvailability();
    		}
    
    	}
    	
    	public static void main(String[] args) {
    
    		Runner run = new Runner();
    	}
    }
    And the other class:

    Java Code:
    public class Play extends Runner {
    	
    	public void checkAvailability() {
    	}
    }
    And the errors:
    Exception in thread "main" java.lang.StackOverflowError
    at apple.awt.CGraphicsConfig.getBounds(Native Method)
    at apple.awt.CGraphicsConfig.getBounds(CGraphicsConfi g.java:258)
    at java.awt.Window.init(Window.java:394)
    at java.awt.Window.<init>(Window.java:433)
    at java.awt.Frame.<init>(Frame.java:403)
    at javax.swing.JFrame.<init>(JFrame.java:202)
    at game_one_N_and_C.Runner.<init>(Runner.java:34)
    at game_one_N_and_C.Play.<init>(Play.java:3)
    at game_one_N_and_C.Runner.<init>(Runner.java:17)
    at game_one_N_and_C.Play.<init>(Play.java:3)
    at game_one_N_and_C.Runner.<init>(Runner.java:17)

    last two errors repeat themselves

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    What do you think this is going to do?

    Java Code:
    public class Runner {
    	//...
    	Play p = new Play(); //this is causing the problem
    	//...
    Java Code:
    public class Play extends Runner {
    Can you see how this will cause infinite recursion (or until memory runs out)?
    Edit: To elaborate, Every when you create a Runner object, it will create a new Play object, but since Play extends Runner, this will create a new Runner super object which creates a new Play object, which,... ad infinitum...

    I think that you're misusing inheritance. Why does Play extend Runner? Does Play behave as a Runner? Do you really absolutely positively have to have Play extend Runner and if so why?
    Last edited by Fubarable; 07-27-2011 at 10:22 PM.

  10. #10
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default

    Ok thanks, I definitely don't understand inheritance well enough. I will read up on some tutorials on it asap.
    The reason I made play extend runner was just so that it could have access to all its variables. But it would be better to just import the variables I need to use in the methods right?

    EDIT: Its working fine now, thank you and everyone else who posted, I really appreciate it!
    Last edited by Guy; 07-27-2011 at 10:25 PM.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Guy View Post
    Ok thanks, I definitely don't understand inheritance well enough. I will read up on some tutorials on it asap.
    The reason I made play extend runner was just so that it could have access to all its variables. But it would be better to just import the variables I need to use in the methods right?
    Yep, that's not a reason to use inheritance. You only have Play inherit Runner if Play actually is a specific type of Runner. You don't use inheritance just to be able to use another class's variables. Solution: don't have Play extend Runner.

  12. #12
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Here are some rules of thumb I use.

    In the class that contains main I try to keep it as simple as possible and make it the only static method in the class. I then immediately instantiate my class and transition out of static main into non static methods.
    Fubarable likes this.

  13. #13
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

Similar Threads

  1. Garbage Collection - Self reference, static and non-static.
    By garyiskidding in forum Advanced Java
    Replies: 10
    Last Post: 03-17-2011, 11:22 AM
  2. Replies: 10
    Last Post: 11-14-2010, 07:06 AM
  3. static variable /pass by reference
    By katturv in forum New To Java
    Replies: 15
    Last Post: 10-03-2010, 08:17 AM
  4. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  5. Replies: 3
    Last Post: 08-17-2009, 10:59 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •