How to avoid non-static method?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • linksterman
    New Member
    • Jul 2007
    • 38

    How to avoid non-static method?

    Sorry for making another thread ( I actually solved my other one).

    but this section of code I really need help on:

    Code:
        public void shutDown()
        {
            out.println("close"); 
            out.flush();
            System.exit(0);
        }
        public static void main(String[] args) 
        {
            ClientGUI mainWindow = new ClientGUI();
            mainWindow.pack();
            mainWindow.addWindowListener(new WindowAdapter() 
            {public void windowClosing(WindowEvent e) 
                {shutDown();}
            });
            mainWindow.setResizable(false);
            mainWindow.setVisible(true);
        }
    how do I get rid of the non-static method error? I searched but came up empty handed. It is really important I do those steps on shut down.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    You could just put the code in the addWindowListen er instead of calling shutDown(). Alternatively, you could (probably) make shutDown() static.

    Comment

    • linksterman
      New Member
      • Jul 2007
      • 38

      #3
      Originally posted by Laharl
      You could just put the code in the addWindowListen er instead of calling shutDown(). Alternatively, you could (probably) make shutDown() static.
      It i do that then it says "non-static variable out cannot be referenced from a static context"

      in both ways that you mentioned.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by linksterman
        It i do that then it says "non-static variable out cannot be referenced from a static context"

        in both ways that you mentioned.
        You get get around that be defining a proper WindowAdapter subclass and make out a field in it.

        Also, you may be interesting in shutdown hooks:

        Comment

        Working...