Wuppy’s Minecraft Forge Modding Tutorials for 1.7: Basic File Part 3: Classes and Keywords

In the last tutorial I explained the @Mod line and how it works from the file you got in your project with the basic Eclipse setup. In this tutorial I will explain something about classes and some important keywords. If you already know Java you may want to skip this tutorial. The code from the example mod.

 

After the @Mod line you get to the public class ExampleMod line. To explain this bit of code you need to know a bit about Java.

In Java code is grouped up in several ways. The biggest group is a folder in which you store files that have a general common purpose. In a file can be multiple classes, but for modding we will keep it to one class in every file. A class is a group of code that usually does a single thing. In this case the ExampleMod file contains all the code that is required for a proper mod. However, not all of the code you are going to write is going to be in the ExampleMod file. It will somehow be connected to this file though. To tell Java where there is a class you use the following code

Public means that it can be seen from everywhere in the code. If you are going to work on really complicated things you may want to have nonpublic files, but for modding that is generally not required. The word class makes sure Java knows that the following lines are going to be a group of code. ExampleMod is the name of the class. Next Java needs to know which code is part of the ExampleMod class. This is done with the 2 brackets below. The first one means the start of the code and the second the end.

Now that you know how a class works, you should know how variables or objects work. In an Object Orientated Programming language (OOP) such as Java pieces of data are kept together in an Object. The class ExampleMod can be turned into an Object somewhere else to access things in there that the code requires. This is what Forge will do. Forge needs to be able to access things though. The thing it will access is usually a method, an object or a variable.

Objects and variables are basically two words for the same thing, but they are used slightly differently. Variables are standard data types which include integers, doubles, floats, chars and a few other number types. You will be using those in other parts of the code. Variables work the same as Objects. Objects are nonstandard data types. A good example of this is the String which we use for the mod id and the version. A String is a bunch of letters packed together in the right order and is very often used in programming.

To use an object there are a few keywords you can use to change them slightly. Public means that the object can be used everywhere. Static (or non-static by not having that word) changes the way it can be accessed. If it is static it can be used as seen in the @Mod line. If it isn’t static you require an object of the class. The next word is final. This means that it can’t be changed anywhere in the code which is a good idea for variables such as the name and version of a mod. The fourth word on the variable line is String, this tells Java to create a String variable. After the String we get the variable name. The variable name is the name you access the value with. This variable name is all capitals, which you should always do if it is a final variable (don’t if it isn’t final). It doesn’t break your code if you don’t, but it makes it better code and also easier to read. After the variable name we set the variable to something. If you want to create a String variable you use the quotation marks to start and end the word you want to be in the String. The quotations itself don’t get included in it though. The semicolon on the end tells Java that the line has ended.

In the next tutorial I will explain the next few lines of this file.

 

For a more detailed explanation and much more, check out my book Sams Teach Yourself Mod Development for Minecraft in 24 Hours.

STY Minecraft Mod Development Book

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">