How to call a variable in another class?

Discussion in 'Spigot Plugin Development' started by Orangemaster123, May 28, 2017.

  1. I have a class which has the variable
    Code (Text):
    Inventory guimain = Bukkit.createInventory(null, 45, "EazyHub");
     
    I want the variable to be called here so the player opens the Inventory.
    Code (Text):

    public void click(PlayerInteractEvent e) {

    ItemStack menu = nameItem(Material.SPECKLED_MELON, ChatColor.GREEN + "HUB MENU");

    Player player = e.getPlayer();

    if(player.getItemInHand().equals(menu));

    player.openInventory(guimain);

    }
     
    Any ideas how i can call the variable?
     
  2. Get the Instance of that class and get the method.
     
  3. To accomplish what you want to do you would make the variable static. Although be warned, this is usually not the best way of accessing shared variables.
     
    • Optimistic Optimistic x 3
    • Funny Funny x 1
  4. Mas

    Mas

    Don't teach bad habits. It's clear the OP has not properly learnt Java

    @Orangemaster123
    This is basic stuff which you would be able to do should you have taken time to learn Java prior to making plugins.
    Here are some links to help you learn:
    https://docs.oracle.com/javase/tutorial/
    https://docs.oracle.com/javase/tutorial/java/javaOO
    https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
     
  5. You shouldn't be working with this API because you evidently have no basic knowledge of Java or objects. I am not being "mean" here I am simply advising you that before you get used to using things from this API, take the time to learn how Java works first or even basic logic. You will find yourself much better off when you start using the API when you have a Java background.

    Look at the above provided links to assist you.

    As for your answer, you would need to create an instance of that class to access its methods. This instance is called an object. Learning about how these work and what they are will help you understand what you are doing
     
    • Like Like x 1
  6. You are correct that habits such as static abuse should not be taught, but when given the correct warnings it should be fine to explain what they want to do while also explaining that what they want to do isn't the way that it should be done
     
    • Funny Funny x 1
  7. Mas

    Mas

    I don't really agree with this.

    By telling someone who doesn't really know Java a bad way of doing something, no matter how much you warn them, they're going to want to take the easier route, especially if they never bothered to learn the language. It's much better to tell them how to learn properly than throwing solutions at them which they will abuse and won't understand.

    As fair as the OP is concerned, they might start to think static is a magic keyword which let's you access things from anywhere. Sounds great, doesn't it? But without proper understanding they're making it very hard for them to improve.
     
  8. You're right again, I didn't think about it that way. I started to steer beginner away from bad practices but it does make logical sense that someone who has limited knowledge of the language would prefer to take the easier path.
     
    • Like Like x 1
  9. Thanks for the reply, I do have some basic java knowledge but obvioulsy not enough :p
     
  10. @ItsMas_
    Thanks for the reply, your right I don't have the basic java knowledge i should have , but why is static bad?
     
  11. Mas

    Mas

    It's not inherently bad, it just has it's uses which you should stick to.
    Static methods and fields exist in the class iteself, not in an instance of the class, going against OOP principles.
    Static is great for utility methods or constants which you want to reference from lots of places, but if just trying to use a method/object from another class, you should be passing instances.

    The links I posted above should help, along with this: http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
    You will want to pass an instance of a class through the constructor of another class in order to use its methods and fields in the other class. That probably wasn't the clearest explanation.

    It's nice to see someone who actually wants to learn for once, good on you and good luck! :)
     
    • Like Like x 1
  12. static isn't always bad but can easily be abused.
    There are quite a few reasons such as it not following Object Oriented design.
    Cases where static may be useful is when you are declaring constants.
    Usually people would recommend creating instances of an object and using getter and setter methods to make your code more Object Oriented.
     
  13. Thank You! I thought i knew enough basics of java to start plugin development, as I thought that would teach me java as well. But i was clearly wrong so i'm gonna start watching videos on basic java. :)
     
    • Like Like x 1
  14. Its not very smart to bypass the OOP features in a OOP Programming language ;)

    Im now talking from my pov. I used static as a beginner almost everywhere but the side effect is that you have cluttered unreadable code and you are forced to use static then everywhere making it a big mess. But then i switched and learnt more about java which was the best decision i could possibly made!
     
    • Like Like x 1
  15. You have probably already been linked twenty times to different places, and you shoul defiantly read it all. However, I am going to demonstrate an example.

    Let's say you have the class Dog (normally you would have an interface/abstraction defining this object such as Animal).
    Code (Text):
    public class Dog {

    }
    Simple. Now, let's pass in our variables and a constructor.
    Code (Text):
    public class Dog {

      private String name;
      private int age;

      public Dog(String name, int age) {
        this.name = name;
        this.age = age;
      }
    }
    Why private? I want to control the modification/access (can't hide from reflection, though) of the variables so other programs can't just call Dog#age = 15.

    What you need next is something called getters and setters. They are methods which allows you to modify these variables.
    Code (Text):
    public class Dog {

      private String name;
      private int age;

      public Dog(String name, int age) {
        this.name = name;
        this.age = age;
      }

      // Getters
      public String getName() { return this.name }
      public int getAge() { return this.age }

      // Setters
      public void setAge(int newAge) { this.age = newAge }
    }
    Now in another class, I can create an instance of Dog, and call the getters/setters.
    Code (Text):
    public class MyClazz {

      private void anyMethod() {
        Dog puppy = new Dog("Lucky", 5); // new instance created
        String puppysName = puppy.getName(); // I got the "name" variable!
      }

    }
    You can keep references to objects such as these and have getters for them.
     
    • Informative Informative x 1
  16. Just gonna say that Kotlin automatically creates getter and setter methods
    Code (Scala):

    public class Dog(var name: String, var age: Int) // In Kotlin, that's all you need
     
    Code (Java):

    public class MyClazz{
        private void anyMethod(){
            Dog puppy = new Dog("Lucky", 5);
            String puppyName = puppy.getName(); // Kotlin auto generated a getter method
        }
    }
     
    Once OP has become comfortable with Java and OOP design, I would suggest that they look into Kotlin as it can easily make programming easier
     
    #16 spammy23, May 29, 2017
    Last edited: May 29, 2017