Skip to content

Instantly share code, notes, and snippets.

@ducc
Last active August 25, 2022 16:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ducc/de965055a57f178005ab to your computer and use it in GitHub Desktop.
Save ducc/de965055a57f178005ab to your computer and use it in GitHub Desktop.

I spent a while writing this out, so please give it a read and try this out. You'll thank me soon!

Explenations

Why shouldn't you be using static?

Static is not evil, however you are using it wrong. This is known as 'static abuse'. http://stackoverflow.com/questions/1766715/when-not-to-use-the-static-keyword-in-java http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil

I would highly advise checking out the first 20 or so of the new boston's java tutorials. Some may disagree, however I believe that they are very helpful for explaining the basics when you are just stating out with java. He does miss out on some code conventions for java, but this does not condemn his whole tutorial series. Check the playlist out here: https://www.youtube.com/playlist?list=PLFE2CE09D83EE3E28 His video on constructors: https://www.youtube.com/watch?v=tPFuVRbUTwA Correct java code conventions cheat sheet: http://www.cs.bham.ac.uk/internal/courses/java/msc/resources/code-conventions.html

About constructors:

  • The content inside of the constructor is called when you instantiate the class. For example, you could make it run System.out.println("hello"); in the constructor and it would run as soon as that new class is instantiated.
  • Inside of the parameters of the constructor (parameters are what is inside of () that), you can add dependencies. This is known as dependency injection. A use for this would be to inject a class as a dependancy, and then you can access all the public methods and variables that you want from the dependancy.

Basic tutorial

First, you want to make the new class with a constructor, injecting the dependancy of the Main class. In this instance, we are going to use MyOtherClass as your new class, and MyMainClass as the class you want to get the methods & variables from. In a bukkit situation, your Main class is the class that extends JavaPlugin. Note, 'dependency injection' can be used for any class, not just your Main class :)

public class MyOtherClass { // just declaring the new class.

private MyMainClass myMainClass; // creating a variable for the main class that we can use for accessing methods & variables in that class.

public MyOtherClass(MyMainClass myInjectedClass) { // the constructor. If you don't want to inject any dependancies, you can just use MyOtherClass() { /* stuff here */ } this.myMainClass = myInjectedClass; // what this is doing is setting the 'myMainClass' variable to the injected 'myInjectedClass'. * }

}

  • you can use the same variable name, and then refer to it with the 'this' operator, however it's good to keep it simple whilst you are learning!

Once the constructor has been made, you can access all the public methods & variables just by doing... myMainClass.myMethod();

Now to get this to actually work, you want to be using the 'new' operator to create a new instance of that class. I would advise that you do this in your Main class. An example of this is... new MyOtherClass();

Now that doesn't line up with the injected dependencies, so we have to add those into the parameters of our new instance. You do this like... new MyOtherClass(myMainClassVariable);

If you're doing this in the class you are injecting, you can use the 'this' operator to refer to the class that you are in... new MyOtherClass(this);

You can of course assign this to a variable for use else where, e.g. private MyOtherClass myOtherClass = new MyOtherClass(this);

Cool advise for Bukkit implementations

With Bukkit, you should be using your onEnable method! An example of this:

public MyMainClass extends JavaPlugin {

private MyOtherClass myOtherClass;

@Override public void onEnable() { this.myOtherClass = new MyOtherClass(this); }

}

Good luck! If you need help, add me on skype: joepwnsall1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment