Solved java class for?

Discussion in 'Spigot Plugin Development' started by Bruce0203, Oct 12, 2019.

  1. What is goodness of java class?
    I cant understand.
    what is good when i divide class?
    I think many class make more CPU usage and RAM usage.
    I think make one main class and put them all the Function.
  2. some plugins are just too big. if you then put everything in 1 class it becomes unclear
    • Like Like x 1
  3. SteelPhoenix


    More classes doesn't mean more cpu usage. The VM only loads the classes you're using into memory.
    • Like Like x 1
  4. This is an age old question when talking about Object Oriented Programming. My own personal opinion is:
    • having multiple classes makes your code easier to understand and less messy
    • With packages and classes you can "group" code together via packages
    • CPU usage does not necessarily increase (read SteelPhoenix's reply)

    The simple answer is: if everything is in one class adding something new can become incredibly annoying, and while this works for smaller projects a lot of things will start breaking if the project is huge.

    Lets take a real life example: if you reading a book it has chapters & sections. The same should be done with code, also having everything in one class is not a good practice either.

    • Like Like x 1
    • Agree Agree x 1
  5. When building a java application, there is no need to worry too much about memory usage. Java itself is not on the light side of memory usage. Spigot contains thousands of classes. Chunks take much memory.
    Your plugin can safely define 10 or even 100 classes and have them all loaded without any important memory impact.
  6. I feel like these are conflicting statements, perhaps a typo/brain fart?

    My personal take on the matter ties in more closely to SRP (Single responsibility principle), which is one of the SOLID principles. In terms of java classes, a class should only hold one pragmatic functionality, and nothing else (excluding metadata ofc), thus a plugin entirely encased in one class is a severe violation of this principle.

    In terms of raw performance, I’d assume there will be the slightest benefit of bundling everything into one class, but it creates immense difficulty regarding readability and maintainability for any soul unfortunate enough to look at the code, including the author. With that in mind, it’s not even remotely justifiable to create the clusterfuck of a class you’ve described.

    For your own sanity, at least separate it partly into multiple classes; full on SRP isn’t doable for most people, especially new developers. And as a side note, I would highly recommend looking into the other 4 principles in SOLID too.
  7. To list all by name.
    1. Single responsibility principle
    2. Open closed principle
    3. Liskov substitution principle
    4. Interfaces segregation principle
    5. Dependency inversion principle
    • Winner Winner x 1
  8. All of which are extremely useful when writing clean and maintainable code, however I feel like the other four stray a bit off topic, as they don’t directly relate to the topic at hand, which is why I only mentioned srp (briefly at that).
  9. No.
    Java itself contains already thousands of classes. Spigot adds thousands more. Thousands of classes are loaded already. Chunks also take much memory. So the amount of memory for his 10 or 100 classes, compared to what java, jvm and spigot take, very low.
  10. SteelPhoenix


    If you have to worry about memory usage you wouldn't be running Java lol
    Plugins like MassiveCore even load entire databases in memory just so they can directly access it.
  11. More informations here