Minecraft Blogs / Tutorial

Java Text Tutorial 9 - Multiple Classes

  • 268 views, 1 today
  • 1
  • 0
Cool_Man7's Avatar Cool_Man7
Level 16 : Journeyman Modder
9
And we're back! Welcome to episode 9 of Java Text Tutorials!


Table of Contents:
9. What are multiple classes?
     9.1 Using Multiple Classes



9. What are multiple classes?
Multiple classes are essential to any coder. They are multiple classes where code can go in to different areas. Rather than having all of your code in one class, you can have it in many classes. Not only does it server as an organizer, but what you can do with classes make them a bit more. Here is an example of a class:

public class justAnotherClass {
}

Rather than having just that one class to do everything, you can split it up into multiple sections. Every smart programmer knows that using more than one class is essential to any non-basic programs (Example of a basic program would be a basic calculator (that will be covered in a later tutorial)).

9.1 Using Multiple Classes
Using them are very easy, you do the same steps we did in episode 2. Right click on the package you have (should be something like com.yourname.projectname) and click new, class. Name your class whatever you desire. Then simply hit enter and the class will create itself. Now, in Java, the compiler searches for your main method and runs whatever is inside of there. So, inside of your main method type:

public static void main(String[] args) {
     whateverYourClassNameIs e = new whateverYourClassNameIs();
}

You will get a problem, but ignore it for now. What this is doing is creating what is called an object. Whatever you named your class will go in the whateverYourClassNameIs areas. Now to actually do something with this, we need to create a method inside of your other class. To do this, go to your other class and write a method in there. Remember:

- Access Modifier (Sorry about spelling last tutorial)
- void (We are not returning anything yet.)
- methodName (camelCase)
- parenthesis ()
- curly brackets {}

Then, write a simple println statement in your new method to confirm that it works.

System.out.println("Can you see me?");

If we can see Can you see me? in the console then we know it worked properly. Go back to your first class, with the main method, and under the object type:

e.whateverYourMethodNameIs();

What this is doing is calling the method that you made in the second class. Remember to change whateverYourMethodNameIs to whatever you named your method.

In the console we should get:
undefined
Success! We have successfully created an object and made it run whatever was in the other method. 

In the next tutorial we will build a Basic Calculator.
Tags

Create an account or sign in to comment.

Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome