Get Location in other class?

Discussion in 'Spigot Plugin Development' started by minimidreamer, Jan 7, 2020.

  1. Hello, is there a way, to use a simple location
    Code (Java):
    Location location = new Location(world, x, y, z, yaw, pitch);
    In a other class?

    I want to teleport me with a GUI click
     
  2. make it static lol.

    Code (Text):
    public static Location LOCATION = new Location(world, x, y, z, yaw, pitch);

    Yourclass.LOCATION; //Access
     
    • Funny Funny x 2
    • Optimistic Optimistic x 1
  3. SteelPhoenix

    Moderator

    You shouldn't advise people to abuse static like that.

    (unless OP wants to use it as a constant in which case it also should be final)
     
    • Agree Agree x 5
  4. https://gyazo.com/db350f9e199dfc8a09a40635e0a397a4
    Click the link and you will see the error
     
  5. „Remove invalid modifiers“

    PS: If it were so easy with just a static, I would not ask. That was the first thing I did
     
    • Funny Funny x 1
  6. "Remove invalid modifiers" probably means that you defined this variable somewhere where you shouldn't. It's hard to tell tho without any context what so ever
     
  7. There's a reason he said that you know...
     
  8. MrGeneralQ

    MrGeneralQ Previously qintinator
    Supporter

    Making static is not just making it available from all classes. Dependency injection is the only correct (proper) approach.

    Static makes it unique and only callable by class name. And that is a bad approach.

    A good usecase for static could be for for example messages that are hardcoded or singleton paterns
     
    • Agree Agree x 3
    • Funny Funny x 1
  9. So what can you can recommend?
     
  10. Since you do not provide any information one can only guess.
    location
    a) is defined inside a method, in this case:

    Code (Java):
    Class classWhereTheMethodIsInside {

        private Location location;
       
        public x methodThatContainsLocation(...) {
            this.location = new Location(world, x, y, z, yaw, pitch);
        }

        public Location getLoation() {
            return this.location;
        }

    }
    b) is not defined inside class

    please no

    c) is defined inside an interface

    please no

    d) is defined inside an enum

    go to a)

    e) anything I missed that could explain an invalid modifier?
     
  11. Post both classes here. >_>
     
  12. The problem here is the lack of knowledge of basic Java and OOP. Even after s/he was told to make it static (which is btw the worst approach) s/he couldn't understand how to use it because of lack of knowledge and understanding OOP (you can't use this keyword inside methods, just on fields of objects)
    What you should use is dependency injection. (Just google it, it's pretty simple)
     
    • Agree Agree x 2
  13. Lack of knowledge AND not explaining what the problem’s context is. If you don’t know how to code and want to learn from others then give all the information and let’s discuss it.
     
  14. I know, that a static is the worst approach @StylexTV recommend it.
     
  15. Dude dont blame me you. You asked for it
     
    • Funny Funny x 1
  16. MrGeneralQ

    MrGeneralQ Previously qintinator
    Supporter

    #17 MrGeneralQ, Jan 8, 2020
    Last edited: Jan 8, 2020