How to get objects from another class without them being static?

Discussion in 'Plugin Development' started by Smex, Jan 26, 2012.

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

    Smex

    Example

    Code:
    public class A{
      int a = 2;
    }
     
     
    public class B{
     
    }
    Now I want to get the int without haveing to type the class name A each time I want it to access.
    I want to know how you can do this, because in our project DragonTravel, it gets kinda messy when have to type the long class names, each time we want to get an object from another class.
    What I am trying is this:

    Code:
    public class A{
      int a = 2;
    }
     
     
    public class B{
    A a;
    System.out.println(a);
    }
    ^
    I did not test this code snippets.

    Basically, when in DragonTravel I am doing a command it gives me now everytime a NullPointerException on line 215 , when I just do /dt in this class: http://pastebin.com/NMDumbCY
    This is the DragonTravelCommandHandlers class: http://pastebin.com/C3wGVBLZ

    It also gives me the exception if it is written: DragonTravelCommandHandlers cmdhandle;, without the = new DragonTravelCommandHandlers();

    How do I create an object from which I can use the methods in other classes but without the methods being static?
     
  2. Offline

    coldandtired

    The error is because you don't initialize the cmd variable in the handler class.

    Add a constructor to the handler class which takes an instance of the main class as a parameter and set it from that.
     
  3. Code:
    public class A
    {
    public int someInt = 2;
    }
     
     
    public class B
    {
    A aobject;
     
    public B()
    {
    this.aobject = new A();
    this.aobject.someInt += 120; // do what ever you want with that.
     
    // or you can also do soemthing like this
    int myLocalInt = this.aobject.someInt;
    myLocalInt *= 34; // do what you want with this, without any long classnames etc. NOTE: Any change you do to myLocalInt will also apply to this.aobject.someInt!
    }
    }
     
  4. Offline

    SirTyler

    to add to that,
    Code:java
    1.  
    2. public class A
    3. {
    4. public int someInt = 2;
    5. public static function getSomeInt():int {
    6. return someInt;
    7. }
    8. }
    9.  
    10.  
    11. public class B
    12. {
    13. A aobject;
    14.  
    15. public B()
    16. {
    17. aobject = new A();
    18. private localInt = aobject.getSomeInt(); //returns someInt without modifying it.
    19. }
    20.  
     
  5. Offline

    Smex

    I have really problems to understand this, what I did now was making constructors for each other class
    I want a object from:

    Code:
    DragonTravelMain main;
        DragonTravelFunctions funct;
        DragonTravelCommandHandlers cmdhandle;
     
        private String entername = "Enter a destination/station name!";
        public String travel = "dt.travel";
        public String waypoint = "dt.waypoint";
     
        private ChatColor red = ChatColor.RED;
        private ChatColor white = ChatColor.WHITE;
     
        public DragonTravelCommands(DragonTravelMain main){
            this.main = main;
        }
     
        public DragonTravelCommands(DragonTravelCommandHandlers cmdhandle){
            this.cmdhandle = cmdhandle;
        }
     
        public DragonTravelCommands(DragonTravelFunctions funct){
            this.funct = funct;
        }
    I made also constructors in the other class so they are linked(?).

    Code:
    public class DragonTravelCommandHandlers{
     
        DragonTravelCommands cmd;
     
        private ChatColor red = ChatColor.RED;
        private ChatColor white = ChatColor.WHITE;
        private ChatColor gold = ChatColor.GOLD;
     
        public DragonTravelCommandHandlers(DragonTravelCommands plugin){
            this.cmd = plugin;
        }
    I am really confused.
     
  6. €dit: The construction of objects can get a little bit wierd^^
    An object is constructed exactly one single time, by using ONE of its contructors. If you declare 3 constructors like you did, you have the choice to construct the object by 3 different ways. If all classes of your 3 constructors are need, you can combine them in one constructor (example is below). Or you make sure, that every single object your command class depends on is initialized. Take a look on the getter/setter at the bottom of the class i posted. The first construction attemp with no parameters uses the getter and setter to bring the objects into your command object.

    That would be my first try:

    Code:
    class DragonTravelCommands
    {
        private DragonTravelMain main;
        private DragonTravelFunctions funct;
        private DragonTravelCommandHandlers cmdhandle;
     
        private String entername = "Enter a destination/station name!";
        public String travel = "dt.travel";
        public String waypoint = "dt.waypoint";
     
        //Simple empty constructor for this obj
        public DragonTravelCommands()
        {
            /* if you you this constructor, you have to add the other "components" by hand
            *
            * So, when you do:
            * DragonTravelCommands commands = new DragonTravelCommands();
     
            * You also have to do...
            * commands.setMain(new DragonTravelMain() // or where ever you get this object from);
            * commands.setFunct(new DragonTravelFunctions() // or where ever you get this object from);
            * commands.setCommandHandlers(new DragonTravelCommandHandlers() // or where ever you get this object from);
            *
            * ..before you can use the command class.
            *
            * command.doSomething();
            */
        }
     
        // Extended constructor, where you overgive the objects directly in the class
        public DragonTravelCommands(DragonTravelMain dtmain, DragonTravelFunctions dtfunct,DragonTravelCommandHandlers dtcmdhandle)
        {
            /* By this way, you have something like:
            *
            * DragonTravelCommands commands = new DragonTravelCommands(new DragonTravelMain(),new DragonTravelFunctions(),new DragonTravelCommandHandler());
            * command.doSomething();
            */
            this.main = dtmain;
            this.funct = dtfunct;
            this.cmdhandle = dtcmdhandle;
        }
     
        public DragonTravelMain getMain()
        {
            return main;
        }
     
        public void setMain(DragonTravelMain main)
        {
            this.main = main;
        }
     
        public DragonTravelCommandHandlers getCmdhandle()
        {
            return cmdhandle;
        }
     
        public void setCmdhandle(DragonTravelCommandHandlers cmdhandle)
        {
            this.cmdhandle = cmdhandle;
        }
     
        public String getTravel()
        {
            return travel;
        }
     
        public void setTravel(String travel)
        {
            this.travel = travel;
        }
     
    }
     
Thread Status:
Not open for further replies.

Share This Page