Solved Public not doing it's job?

Discussion in 'Spigot Plugin Development' started by Igasi, Apr 17, 2017.

  1. So I was told "public" means that all classes in the project can use it, right?

    Well guess what? Over here
    Code (Text):


        public String weakzombie = ChatColor.RED + "[" + ChatColor.GOLD + "Lv1" + ChatColor.RED + "] " + ChatColor.GOLD + "Weak Zombie" + ChatColor.RED + "[" + ChatColor.GOLD + "Neutral" + ChatColor.RED + "]";
    We have a public string that is underlined in red here:
    Code (Text):

    public void onEnable(){
        Bukkit.getPluginManager().registerEvents(this, this);
    public void ripEntity(EntityDeathEvent e){
    Just... why?

    If I misunderstood what public means, can someone tell me how I can make a string usable in other classes?
  2. Please learn java.
    • Like Like x 2
  3. If you want to just access the strings make it static. Static means it doesn't have to create a new Instance for the class each time, so it's useful for functions like calculations . if you just have it public, you need to define the class first with ClassName cn = new ClassName(). and then you can access it by cn.weakzombie. For this I would make it static and call it by ClassName.weakzombie
  4. Use encapsulation
    • Agree Agree x 1
  5. Public is doing its job, your not doing your job by learning java
  6. I watched like 2 tutorials and read a document, 2 of them said you shouldnt learn static cuz it messy (The other one said something that I don't understand) and all 3 said public means that that other classes can use it (In the example of a method)

    why does everyone tell me to learn java when I tried?
  7. Not sure what tutorials you have watched but they aren't good as static has a great use.
    • Optimistic Optimistic x 2
    • Funny Funny x 1
  8. public is a modifier that states if any other class can see the field/method. private is a modifier that states that only the class itself can access this field/method. protected is just like private but subclasses can also access it. Nothing means it is package protected, which means that only classes in that package an see it.

    I dont remember where the other one was

    Also I found this by googling something which brought me to a thread in and the most ironic thing is how many comments said it was a good tutorial.
  10. yeah exactly what I was told

    If you tell anyone who is new to programming that they'll be as clueless as I was (I was told this like 2 months ago)
  11. What is the use....?

    Only to use it on types of classes while most people abuse it for a quick way to get a value instead of using a more object oriented approach. There are uses but most time people confuse them and leads to really bad organization that goes against all of the rules if Java

    Read an actual book written by professionals instead of watching videos from random people on YouTube who have barely any experience themselves
  12. make the weakzombie thing static, so all classes can see it without an instance of that class
  13. There aren't a lot of uses but for a function like StringUtils where you add color codes to the string with
    Code (Text):
    public static String toColor(String s) {
      return ChatColor.translateAlternateColorCodes('&', s);
  14. yeah thats what I did
  15. Allow me to add some confusion into the mix:
    There's a couple of ways to make something publicly accessible to the entire project. One of these is baked into most modern programming languages, and there's two popular design patterns (logical consequences of these languages).

    First, we have static which is what everyone's describing above. Be careful when using the "static" keyword, as it can lead to bad programming behavior and practice in general. A common case of the abuse of "static" is what's called a "static graveyard" or a large class that holds nothing but static variables and methods that all other classes use. The second-most common abuse case doesn't really have a name; it's just making your entire project static. Egh. static has its uses, but use it sparingly.

    Second, we have a design pattern called the Singleton. This is quite common in Java- it has only one static method (usually called getInstance()) and returns an internal copy of itself that it creates and stores at startup. A common scenario of these is the "lazy initialize" Singleton, where instead of initializing and storing a copy of itself on startup, it doesn't do so until the first time it's accessed. This can save memory and CPU if it isn't actually used (or isn't used until much later)

    The third is my favorite pattern, called the "service locator". This is usually an entire static class with a few methods. Most games have what we might call "services" - for example, and audio engine might be a service. Same with an input engine or physics engine. These can be thought of as "services" that your other classes hook into. A common use-case for the service locator pattern is indeed an audio engine, where a specific class may need to play an audio effect. Service locators intentionally make things more difficult to set up to begin with (as the class needs its own copy of the value provided by the locator) since it's far too easy to hook the audio engine into, say, the physics engine. It drives you to think about what you're doing before you do it.


    Speaking of (directed at the OP), it would probably be a good idea for you to look at a few more Java tutorials before diving into creating new entities. Maybe try something simple first, like flipping a player 180 degrees? You might be surprised at how difficult even that can be.
    • Like Like x 1