I noticed that a few people on this forum are into the copy/paste methods. So I am creating this so people know enough java to figure out stuff by themselves (e.g "What does interface mean?").This thread will have all the java that you will need to know for minecraft modding!Some good words from Mazetar:
Also a general programming thing which also applies to java (And yes it applies to modding!)- Plan & Design before implement -> Programming is more about that than about typing code, the code typing part is the smallest bit of what you do and it's VITAL that newcomers understand this from the start, else they will never pass the tutorial stage.
Taking a look at a java class
public class MyClass extends BiggerClass implements Interface{public static int myInt;public static MyClass instance;public MyClass(int myInt){this.myInt = myIntthis.instance = new MyClass(myInt);}public static int add(int a, int B){return a+b;}}
public class MyClass: This is the declaration of the class, telling java "I am a class!"
extends BiggerClass: this is optional. When a class a extends class b, then the class a has all the properties of class b. You can only extend a class with only one other class.
implements Interface: once again, optional. If you implement something, it has to be an interface (declared in a different file, like a class: "public interface Interface{..."), more on those later.
{: This shows that the class contents start here. Every open bracket ends with a closing bracket, or else you will get an error (open bracket is {, closing bracket is })
public static int myInt;: this declares your first variable! A variable is a value that is stored under a name. "public static" means that it can be accessed by doing MyClass.myInt, int is a primitive data type, which is a data type native to java. int stands for Integer, any whole number(under 4,294,967,295). You can assign values to it by typing myInt = 69; (Note that at the end of ANY SINGULAR STATEMENT, there has to be a semicolon). The other primative data types are byte, short, long, float, double, boolean, and char. Get more info about them here: http://docs.oracle.c.../datatypes.html
public static MyClass instance;: variables can also be classes, which is what classes are for. In this situation, we are making a new MyClass called instance. At first, instance is going to be equal to null, or nothing.
public MyClass(int myInt){: this is called a Constructor This is how a new MyClass is made. The function takes in one argument: an int. You could make it have any name, it does not really matter. It may make more sense later. NOTE THAT THIS IS A BLOCK STATEMENT, THIS DOES NOT END IN A SEMICOLON, BUT OPEN BRACKET. YOU WILL HAVE TO EVENTUALLY END THIS WITH A CLOSING BRACKET.
this.myInt = myInt;: So in our class, we have two myInt's. If I just said myInt=myInt, it would take the myInt from the constructor arguments and set it to itself. The this.myInt says "OK, lets get the myInt from the class", and sets it to the myInt of the arguments.
this.instance = new MyClass(myInt);: Now the constructor will make sense! To make a new anything that is not a primitive type, you need to make a new Object. You do this by assigning the variable to a new whatever the class it is. So we did new MyClass(myInt). Ok, lets look at this. "new" means that we are making a new Object, MyClass is the class, (myInt) is the argument. Even if the constructor has no arguments, you would say "new ThisClass()".
public static int add(int a, int b: function time! This declares a function, or a thing that does something. the int part is the return type. The return type is basically what the function will spit out. So you could say "myInt = add(9, 6)" but not "instance = add(9, 6)". Functions are block statements.
return a+b;: this is the return statement. Once the function returns something, it will stop. So this function will spit out a+b.Well thats it! Please comment on how I could improve!
Interfaces, Abstract, Conditional phrases, and more!
Alrighty-ho. Lets get into this.So, I'm going to start out by saying "what is an interface?". Well, I'll give you an example.
public interface Player{public void dig();public void eat(Food food);public int health = 20;}
So an interface is kind of like a library. The way you get an interface into a class is this:
public class MinecraftPlayer implements Player
So minecraft player would have the dig and eat function, as well as the health variable, which equals 20. The only downside is that you cannot reassign the variable, so it will stay 20 the whole time.So that is interfaces in a nutshell. Usually, minecraft interfaces start with an "I", such as IMob or IEntityThrowable.So, due to our crappy interface, I am going to introduce abstract classes and function. An abstract method is a method that is not clarified by what it does. An example is this:
abstract void eat(Food food);
if a class has abstract methods in it, the class must be abstract:
public abstract class MinecraftPlayer{}
The benifit of abstractness is that unlike interfaces, if a class extends an abstract class, that class can change the value of the variables.More soon! I dont have time for any more
As I don't see it in the future list I'm going to add these fundamental things as suggestions:
Inheritance, Polymorphism, packages, arrays, method overloading,
overriding, abstract (what it is and when to use and NOT use it), static, virtual, references to objects and Garbage Collection.
Code Organization, Code conventions etc. All of these are at the most basic level of java, so they should each be added in my opinion
Also a general programming thing which also applies to java (And yes it applies to modding!)
- Plan & Design before implement -> Programming is more about that than about typing code, the code typing part is the smallest bit of what you do and it's VITAL that newcomers understand this from the start, else they will never pass the tutorial stage.
public class MyClass extends BiggerClass implements Interface{public static int myInt;public static MyClass instance;public MyClass(int myInt){this.myInt = myIntthis.instance = new MyClass(myInt);}public static int add(int a, int B){return a+b;}}public class MyClass: This is the declaration of the class, telling java "I am a class!"
extends BiggerClass: this is optional. When a class a extends class b, then the class a has all the properties of class b. You can only extend a class with only one other class.
implements Interface: once again, optional. If you implement something, it has to be an interface (declared in a different file, like a class: "public interface Interface{..."), more on those later.
{: This shows that the class contents start here. Every open bracket ends with a closing bracket, or else you will get an error (open bracket is {, closing bracket is })
public static int myInt;: this declares your first variable! A variable is a value that is stored under a name. "public static" means that it can be accessed by doing MyClass.myInt, int is a primitive data type, which is a data type native to java. int stands for Integer, any whole number(under 4,294,967,295). You can assign values to it by typing myInt = 69; (Note that at the end of ANY SINGULAR STATEMENT, there has to be a semicolon). The other primative data types are byte, short, long, float, double, boolean, and char. Get more info about them here: http://docs.oracle.c.../datatypes.html
public static MyClass instance;: variables can also be classes, which is what classes are for. In this situation, we are making a new MyClass called instance. At first, instance is going to be equal to null, or nothing.
public MyClass(int myInt){: this is called a Constructor This is how a new MyClass is made. The function takes in one argument: an int. You could make it have any name, it does not really matter. It may make more sense later. NOTE THAT THIS IS A BLOCK STATEMENT, THIS DOES NOT END IN A SEMICOLON, BUT OPEN BRACKET. YOU WILL HAVE TO EVENTUALLY END THIS WITH A CLOSING BRACKET.
this.myInt = myInt;: So in our class, we have two myInt's. If I just said myInt=myInt, it would take the myInt from the constructor arguments and set it to itself. The this.myInt says "OK, lets get the myInt from the class", and sets it to the myInt of the arguments.
this.instance = new MyClass(myInt);: Now the constructor will make sense! To make a new anything that is not a primitive type, you need to make a new Object. You do this by assigning the variable to a new whatever the class it is. So we did new MyClass(myInt). Ok, lets look at this. "new" means that we are making a new Object, MyClass is the class, (myInt) is the argument. Even if the constructor has no arguments, you would say "new ThisClass()".
public static int add(int a, int b: function time! This declares a function, or a thing that does something. the int part is the return type. The return type is basically what the function will spit out. So you could say "myInt = add(9, 6)" but not "instance = add(9, 6)". Functions are block statements.
return a+b;: this is the return statement. Once the function returns something, it will stop. So this function will spit out a+b.Well thats it! Please comment on how I could improve!
public interface Player{public void dig();public void eat(Food food);public int health = 20;}So an interface is kind of like a library. The way you get an interface into a class is this:So minecraft player would have the dig and eat function, as well as the health variable, which equals 20. The only downside is that you cannot reassign the variable, so it will stay 20 the whole time.So that is interfaces in a nutshell. Usually, minecraft interfaces start with an "I", such as IMob or IEntityThrowable.So, due to our crappy interface, I am going to introduce abstract classes and function. An abstract method is a method that is not clarified by what it does. An example is this:if a class has abstract methods in it, the class must be abstract:public abstract class MinecraftPlayer{}The benifit of abstractness is that unlike interfaces, if a class extends an abstract class, that class can change the value of the variables.More soon! I dont have time for any moreInheritance, Polymorphism, packages, arrays, method overloading,
overriding, abstract (what it is and when to use and NOT use it), static, virtual, references to objects and Garbage Collection.
Code Organization, Code conventions etc. All of these are at the most basic level of java, so they should each be added in my opinion
Also a general programming thing which also applies to java (And yes it applies to modding!)
- Plan & Design before implement -> Programming is more about that than about typing code, the code typing part is the smallest bit of what you do and it's VITAL that newcomers understand this from the start, else they will never pass the tutorial stage.