Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8553381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:38:18+00:00 2026-06-11T14:38:18+00:00

I have two classes in same package. i have declared a static variable in

  • 0

I have two classes in same package. i have declared a static variable in one class and want to access that variable in another class.

Here is my code in which i have declared the static variable

public class wampusGUI extends javax.swing.JFrame {

    static String userCommand;

    public wampusGUI() {
        initComponents();
    }

    public void setTextArea(String text) {
        displayTextArea.append(text);
    }

    private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
        userCommand = commandText.getText();
    }

    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                wampusGUI w = new wampusGUI();
                w.setVisible(true);
                Game g = new Game(w);
                g.play();
            }
        });
    }
}

Here is the code in which i want to access variable

public class Game {

    private wampusGUI gui;

    public Game(wampusGUI w) {
        world = new World();
        world.start();
        gui = w;
    }

    public void play() {
        gui.setTextArea(welcome());
        gui.setTextArea(describe());
        for (;;) {
            String s = userCommand; // here value should come should 
            System.out.println(userCommand);
            Command c = Command.create(s);
            String r = c.perform(world);
            // is game over?
            if (r == null) {
                break;
            }
            System.out.println(r);
        }
        System.out.println("Game over");
    }
}

However, i can pass the variable from first class as a argument. but the problem is that, when i will run program the value is going null first time, which i dont want. i want when i enter value in textfield then it should go to another class.

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:38:19+00:00Added an answer on June 11, 2026 at 2:38 pm

    I suggest that you use a listener of one sort or another to allow the Game object to listen for and respond to changes in the state of the GUI object. There are several ways to do this, but one of the most elegant and useful I’ve found is to use Swing’s own innate PropertyChangeSupport to allow you to use PropertyChangeListeners. All Swing components will allow you to add a PropertyChangeListener to it. And so I suggest that you do this, that you have Game add one to your WampusGUI class (which should be capitalized) object like so:

    public Game(WampusGUI w) {
      gui = w;
    
      gui.addPropertyChangeListener(new PropertyChangeListener() {
         // ....
      }
    

    This will allow Game to listen for changes in the gui’s state.

    You’ll then want to make the gui’s userCommand String a “bound property” which means giving it a setter method that will fire the property change support notifying all listeners of change. I would do this like so:

    public class WampusGUI extends JFrame {
       public static final String USER_COMMAND = "user command";
       // ....
    
       private void setUserCommand(String userCommand) {
          String oldValue = this.userCommand;
          String newValue = userCommand;
          this.userCommand = userCommand;
          firePropertyChange(USER_COMMAND, oldValue, newValue);
       } 
    

    Then you would only change this String’s value via this setter method:

    private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
      setUserCommand(commandText.getText());
    }
    

    The Game’s property change listener would then respond like so:

      gui.addPropertyChangeListener(new PropertyChangeListener() {
    
         @Override
         public void propertyChange(PropertyChangeEvent pcEvt) {
    
            // is the property being changed the one we're interested in?
            if (WampusGUI.USER_COMMAND.equals(pcEvt.getPropertyName())) {
    
               // get user command:
               String userCommand = pcEvt.getNewValue().toString();
    
               // then we can do with it what we want
               play(userCommand);
    
            }
    
         }
      });
    

    One of the beauties of this technique is that the observed class, the GUI, doesn’t have to have any knowledge about the observer class (the Game). A small runnable example of this is like so:

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    
    import javax.swing.*;
    
    public class WampusGUI extends JFrame {
       public static final String USER_COMMAND = "user command";
       private String userCommand;
       private JTextArea displayTextArea = new JTextArea(10, 30);
       private JTextField commandText = new JTextField(10);
    
       public WampusGUI() {
          initComponents();
       }
    
       private void setUserCommand(String userCommand) {
          String oldValue = this.userCommand;
          String newValue = userCommand;
          this.userCommand = userCommand;
          firePropertyChange(USER_COMMAND, oldValue, newValue);
       }
    
       private void initComponents() {
          displayTextArea.setEditable(false);
          displayTextArea.setFocusable(false);
          JButton enterButton = new JButton("Enter Command");
          enterButton.addActionListener(new ActionListener() {
    
             @Override
             public void actionPerformed(ActionEvent evt) {
                enterButtonActionPerformed(evt);
             }
          });
          JPanel commandPanel = new JPanel(); 
          commandPanel.add(commandText);
          commandPanel.add(Box.createHorizontalStrut(15));
          commandPanel.add(enterButton);
    
          JPanel mainPanel = new JPanel();
          mainPanel.setLayout(new BorderLayout());
          mainPanel.add(new JScrollPane(displayTextArea));
          mainPanel.add(commandPanel, BorderLayout.SOUTH);
          add(mainPanel);
       }
    
       public void setTextArea(String text) {
          displayTextArea.append(text);
       }
    
       private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
          setUserCommand(commandText.getText());
       }
    
       public static void main(String args[]) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                WampusGUI w = new WampusGUI();
                w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                w.pack();
                w.setLocationRelativeTo(null);
                w.setVisible(true);
                Game g = new Game(w);
                g.play();
             }
          });
       }
    }
    
    class Game {
       private WampusGUI gui;
    
       public Game(WampusGUI w) {
          gui = w;
    
          gui.addPropertyChangeListener(new PropertyChangeListener() {
    
             @Override
             public void propertyChange(PropertyChangeEvent pcEvt) {
    
                // is the property being changed the one we're interested in?
                if (WampusGUI.USER_COMMAND.equals(pcEvt.getPropertyName())) {
    
                   // get user command:
                   String userCommand = pcEvt.getNewValue().toString();
    
                   // then we can do with it what we want
                   play(userCommand);
    
                }
             }
          });
       }
    
       public void play() {
          gui.setTextArea("Welcome!\n");
          gui.setTextArea("Please enjoy the game!\n");
       }
    
       public void play(String userCommand) {
          // here we can do what we want with the String. For instance we can display it in the gui:
          gui.setTextArea("User entered: " + userCommand + "\n");
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, and want to include a static instance of one class
I have two classes, one that inherits from the other. The base class is
So my problem is that I have two classes with the same name. One
I have a need for two slightly different classes, that have the same members,
I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes Foo and Bar that Bar extends Foo as below: class
Im having this error where it says that i have two classes of same
I have two classes and I want to pass a (geo-point) variable on touch
I have two Service classes which implement the same interface ServiceClass1 @Service public class
I have two separate classes, ClassA and ClassB, that derive from the same base

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.