Minecraft Blogs / Tutorial

Java Text Tutorial 7 - Parameters

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


Table of Contents
7. What are parameters?
     7.1 How do I use parameters?



7. What are parameters?
Parameters are fairly simple, yet tricky. What a parameter does is give a method information specifically for that method to use. Rather than declaring something like:

public int a;

whenever we want to use a variable inside of a method. Or just putting it inside of the method (which can get sloppy). We can use the () next to the method name to do this. If you remeber the public static main(), that was a method. It had the parameters of String[] args. This helps Java know it is the main method. Parameters look like this:

public void justAnotherMethod(int a) {

}

Did you see the (int a)? That tells Java that the method will use a variable of type int called a. What it is equal to, you cannot set. It is like preparing a method for what is to come. What exactly they do can be tricky. In technical terms, it initiate a variable to be used by the method later. If you need a real-world example, see the start of my plugin zCasino here. Lets use the startSlotMachine as an example. What this is doing is declaring a variable from the type SlotMachineMode (Yes, you can make - in a sense - your own variables. Don't stress too much on that yet.) and calling it state. Ignoring the code inside of the method, it prepares the method to use the variable so you don't need to initiate. Doing that would get sloppy. Also a plus to using parameters, they aren't entirely private. That means that other methods could use the same variable as long as they include it in theirs too (This doesn't include private methods. More on that another tutorial). That is how the other methods work with the variable. That is where you see the state = On; (That is an enum, more on that probably far from now as that is slightly more complex).

7.1 How do I use parameters?
Easy, just like my two examples, you put it inside of the () of the method. If you wanted a method to use an integer called 'a' then this is how it probably would look:

public void aMethod(int a) {

}

You can use other variables too! Try them out for yourself.

In the next tutorial we will talk about method modifyers (public, private, ect.) and a short note about comments.
Tags

Create an account or sign in to comment.

Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome