5 Replies Latest reply: Sep 23, 2006 11:46 AM by 807569 RSS

    Passing value to non-static method from a static method

    807569
      Each time there is an action performed i.e. the user of this hangman game chooses a letter,
      correct is displayed or count is incremented to update the hangman image.
      The problem is the third method - it does not get the icon to display, it gets nothing as
      I have used System.out.println(icon); to see this.

      Please advise how to pass this value.
      public void actionPerformed(ActionEvent e)
      {
           boolean b = false;
      
           String s = e.getActionCommand();
           String string = s.toLowerCase();
      
           b = arrayToPass.indexOf(string) > -1; //Sees if array word has letter in radiobutton in it
      
           //Split the word into an array
           char[] chr = arrayToPass.toCharArray(); // C A T
      
           int capacity = arrayToPass.length();
      
           //ArrayList displayList = new ArrayList(capacity);
      
           int index = arrayToPass.indexOf(string);
      
           if(b == true) //If the word holds that letter
           {
                jtf.setText("Correct");
      
                jl.setText(string);
      
                char character = string.charAt(0);
           }
           else
           {
                count++;
                gameState(count);
           }
      }
      
      public static ImageIcon gameState(int count)
      {
           System.out.println(count);
      
           ImageIcon icon = null;
           //as instance variable
           String[] files = {"C:\\HangMan1.jpg", "C:\\HangMan2.jpg", "C:\\HangMan3.jpg", "C:\\HangMan4.jpg", "C:\\HangMan5.jpg"};
      
           //count starts as 0 of course
           icon = new ImageIcon(files[count]);
           return icon;
      }
      
      public void displayHangMan(ImageIcon icon)
      {
           if(icon != null)
           {
                System.out.println(icon);
                jtp.insertIcon(icon);
                jtf.setText("Wrong");
           }
      }
        • 1. Re: Passing value to non-static method from a static method
          807569
          I don't understand the question, and since nobody else has responded yet I suspect nobody else does either.
          • 2. Re: Passing value to non-static method from a static method
            807569
            Ok ill try and explain better:

            If i had "jtp.insertIcon(icon);" in the "public static ImageIcon gameState(int count) " method it would not work as jtp is a JTextPane and cannot be used in a static method.

            What I am trying to do is passed the value of icon from the static method (the middle one) to the bottom method so that it can be used in a JTextPane - jtp.

            So every time the user selects an incorrect letter the hangman is displayed more and more.

            cheers
            • 3. Re: Passing value to non-static method from a static method
              807569
              If you're in a static context the only want to call non-static code is to instantiate an object. You'll either have to instantiate something or use a reference to an existing object.

              I'd recommend that in general, you want to get out of a static context as quickly as possible. Static code is generally against the spirit of object-oriented programming anyway, but also trying to mix static and non-static code can be a real pain (apart from the occasional static utility method). So in your main() method, instatiate the class that provides the main program code and start doing the real work in there.
              • 4. Re: Passing value to non-static method from a static method
                807569
                Cheers for the help.

                The problem now is that I have to count how many times the action performed method is used to determine how much of the hangman should be shown.

                I think count has to be in a static method i order to work i.e. increment.

                Is there another way of counting how many wrong (non matching letter answers) there are, instead of a static method?

                When you said:
                You'll either have to instantiate something or use a >reference to an existing object.
                was that the other way?
                If so could you please explain a bit more

                Message was edited by:
                John4938
                • 5. Re: Passing value to non-static method from a static method
                  807569
                  Well I don't know what the larger structure of your program is, so I couldn't suggest tweaks to it.

                  But in general...I'd suggest first that you separate your program logic from the GUI. The program logic would be in its own object (and thus would have its own state, and it would not be static). The program logic object's state would include a counter to indicate how many incorrect guesses there are, the correct answer, and the correct letters guessed so far (or, a version of the answer with correct guesses filled it). The object would have a method to guess a letter.

                  The image of the hanged man would really be a GUI widget that has its own internal state consisting of a number. It shows a series of images of parts of a hanged man based on the number. The program logic would have a reference to the GUI widget, and would update its state (the number).

                  The main program would instantiate the program logic object and the GUI elements. There'd be a listener for key or button events representing letter guesses. These would be translated by the listener into the program logic object's guess-a-letter method.