Solved Sharing Variables from Class to Class

Discussion in 'Plugin Development' started by Live2Pwn, Jan 18, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Live2Pwn

    Well, it seems I cannot find the solution to this problem. I need to send a variable from one class to another but I do not know the code the put in either class. Please help!
     
  2. Offline

    CorrieKay

    Pass in the variable as a parameter in a method.
     
  3. Offline

    Live2Pwn

    I am a noob in java. Could I please see some code?
     
  4. Offline

    CorrieKay

  5. Offline

    deApollo

  6. Offline

    Live2Pwn

    I guess I am just having trouble understanding this, I have tried so many different websites and nothing seems to make sense.

    Code:
    public classA{
     
    String msg1 = "Randomness";
     
    /*
    public....
    code i dont understand here
    */
    }
     
    public classB{
    /*
    again code i dont understand
    something msg = new something();
    */
     
    Bukkit.broadcastMessage("Message: " + msg);
    //where msg = msg1 from classA
     
    }
     
  7. Offline

    ZeusAllMighty11

    I used to use static, until I learned you can do like:

    Code:
     
    public class MyClass {
        private Main plugin;
        public int test = 4;
        
        public MyClass(Main pl){
            pl = plugin;
        }
    }
    
    Where Main = main class, and MyClass is just whatever class
     
  8. Offline

    Live2Pwn

    Nvm, found the solution. :D
     
  9. Offline

    caxco93

    another way is to have your variables on your MainClass then send the MainClass as a parameter of the constructor of your other classes.
     
Thread Status:
Not open for further replies.

Share This Page