How do I access variables from the main class from another class (java)?

前端 未结 6 1434
遥遥无期
遥遥无期 2020-12-11 09:06

I\'m making a cookie clicker clone in java to practice my java skills and I have a small problem, I have variables that are declared in the main method that I want to access

相关标签:
6条回答
  • 2020-12-11 09:32

    Your variables should be fields.

    Fields are declared outside of a class's methods and are usually found right below the class declaration. Fields can be accessed by all methods of a class.

    They can also be accessed from other classes (unless they are private) using the dot operator.

    • If a field is marked with static, its class name is used to reference it.
    • If a field is not static, an object of its class is used to reference it.

    Example

        public class Man {
            public String name; //this is a field
            public static String gender = "Male"; //this is a static field
    
            public Man(String newName) {
                name = newName; //assigns the value of a field from within a method
            }
        }
    

    and another class...

    public class Main {
        public static void main(String[] args) {
            Man bob = new Man("Bob");
            System.out.println(bob.name); //referenced from object, prints Bob
            System.out.println(Man.gender); //referenced from class name, prints Male
        }
    }
    

    And to have more control over the access of your fields, you can use getters and setters. Take a read!

    0 讨论(0)
  • 2020-12-11 09:36

    Using fields and their accessor methods. An example here.

    0 讨论(0)
  • 2020-12-11 09:37

    Here, I will give you an example for exactly what you need. In this code you simply just need to set anything you would like to add to actionPerformed as static.

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton; 
    
    public class testJava implements ActionListener {
    
        protected static JButton b; // since this is static you can 
                                    // now access it in other classes
    
        public static void main(String[] args) {
    
    
    
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
    
            if(e.getSource() == b) {
    
                // do stuff here 
    
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-11 09:43
    public class ActionClass {
    {
        private static int clicks;
    
       @Override
        public void actionPerformed(ActionEvent e) {
        clicks++;    
        }
    
          public static void setClicks(int c){
                clicks = c;
            }
    
    
           public static int getClicks(){
                return clicks;
            }
    
    }
    
    
    public class AnyClass {
    {
        // now you have access to your clicks count .
        int clicks = ActionClass.getClicks();
        // set value of clicks
        ActionClass.setClicks(0);
    

    }

    0 讨论(0)
  • 2020-12-11 09:46

    You would have to make the variables public class variables instead of method variables, thereby increasing the scope and visiblity of the variables. Like so:

    public class ActionClass {
    {
        public string MyPublicVariable = "woot";
    
        @Override
        public void actionPerformed(ActionEvent e) {
    
    
            ...
        }
    }
    

    A more popular/recommended way to do this is to use a getter/setter instead of making the variable explicitly public. You would access a private variable through public methods like so:

    public class ActionClass {
    {
        @Override
        public void actionPerformed(ActionEvent e) {
    
            private string MyPublicVariable = "woot";
    
            public void setMyString(string newString){
                MyPublicVariable = newString;
            }
    
            public string getMyString(){
                return MyPublicVariable;
            }
        }
    }
    

    This way, you have more control over what your variables are set to.

    0 讨论(0)
  • 2020-12-11 09:46

    You can pass main class instance reference to another class instance, or register callback. For the first way

    Class MainClass {
      private int mValue;
      public void init() {
         AnotherClass cla = new AnotherClass(this); 
      }
      public void setValue(int value) {mValue = value;}
      public int getValue(){return mValue;}
    }
    
    Class AnotherClass {
      private MainClass mMain;
      public AnotherClass(MainClass ref) {
         mMain = ref;
      }
    
      public void controlValue() {
         if (mMain != null) {
            mMain.setValue(1);
            mMain.getValue();
         }
      }
    }
    

    For the second way 1. declare an interface 2. implement this interface in main class 3. register this implementation to another class. 4. get and set value in another class.

    public interface ClassListener {
        public void setValue(int value);
        public int getValue();
    }
    
    public class MainClass implements ClassListener{
    
        private int mValue;
    
        public void registerListener() {
            AnotherClass cla = new AnotherClass();
            cla.registerListener(this);
        }
    
        @Override
        public void setValue(int value) {
            // TODO Auto-generated method stub
            mValue = value;
        }
    
        @Override
        public int getValue() {
            // TODO Auto-generated method stub
            return mValue;
        }
    }
    
    public class AnotherClass{
    
        private ClassListener mListener;
    
        public void registerListener(ClassListener listener) {
            mListener = listener;
        }
    
        public void controlValue() {
            if (mListener != null) {
                int value = mListener.getValue();
                mListener.setValue(++value);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题