#What to Use Instead of Static

1 messages · Page 1 of 1 (latest)

dapper coyote
#

What is a "proper" or better way to access variables or functions outside of the same class without using static? I know it's generally bad practice to use the static keyword but all of the tutorials I have watched have not shown an alternative.

For example, if I store player's stats in the main class and want to change or read them in a different class, how would I go about doing that?

Main:

public class Main extends JavaPlugin {
    public HashMap<String, Integer> armour = new HashMap<>();
    public HashMap<String, Integer> toughness = new HashMap<>();
    public HashMap<String, Integer> money = new HashMap<>();

public static Main main;

    @Override
    public void onEnable(){
        main = this;
    }
}```

Other class:
```java
public class Events implements Listener {
    @EventHandler
    public void playerShift(PlayerToggleSneakEvent event){
        String uuid = event.getPlayer().getUniqueId().toString();
        Main.main.armour.put(uuid, Main.main.armour.get(uuid) + 1);
        event.getPlayer().sendMessage(Main.main.armour.get(uuid) + "");
    }
}```

This is how I have done it ^. But what I have heard, this is still static abuse. As well as this:

```java
public static Main getInstance(){
    return this;
}```
So if using static is generally bad practice, how do I access anything outside of the same class?
wispy slate
#

You can use dependency injection

dapper coyote
dapper coyote
#

I have a question about dependency injection, though. If I'm constantly using "new Class().something", isn't that way less efficient than simply using "Class.something"?

#

Almost all of my other classes depend on something from my main class so there is a ton of getting and setting it as a variable.

opaque cypress
#

it depends. in most cases, you won't creating a new instance of something just to access a single method

#

and if you want the specifics, then doing Class.something is essentially as efficient as initializing a new class if it has never been called before since the static class needs to also be initialized

dapper coyote
#

Okay. I'm confused on what I should do because all of my methods and variables that I need shared across classes are static. I did try changing some things to use dependency injection but it became a loop of, now this needs that, and that needs this.
So if everything is working perfectly fine, is static really as bad as I hear it to be? And should I take the time to change everything?

opaque cypress
#

it is code smell

#

and just generally indicative of poor design

#

there are valid uses for it, such as utility classes which do not hold state, or utility methods which just make sense being static, but otherwise generally a bad idea

dapper coyote
#

I guess I just don't understand when to and when not to use static despite all of the tutorials I've watched.

#

Like, I'm not going to create multiple objects for player statistics because they are being stored in a hashmap.