Java Reference
In-Depth Information
Is your head full yet? I hope not, because you have a few fun things
left to learn. In this chapter we'll add these abilities to your toolbox:
• Use local variables that exist only within a block
• Use class-level global variables that can be accessed from any-
where in the class
• Keep piles of data in arrays
CHAPTER 7
Use Piles of Variables: Arrays
Now that we can create a plugin command to do something, we need to take
a look at how to remember stuff—in other words, how to better use Java
variables to keep track of values. We'll see when to use which kind of variable
and how to work with piles of data in different ways (things like lists of Player
objects), and we'll build a couple of cool plugins along the way.
Variables and Objects Live in Blocks
Variables live inside of blocks . We've seen and been using blocks all along:
blocks are the bits of code written between braces— { and } .
Java is a “block structured” programming language. It's descended from a
family of languages going back to the ancient Algol of the 1960s, a line that
extends from the mother of all programming languages, C, its children C++
and Objective-C, and down to its half-cousin-stepchild Java. After Java was
around, Microsoft came out with C#, which is (totally coincidentally) nearly
identical to Java. All of these languages work more or less the same way.
In these languages, you work with blocks of code. Things like if statements
work with blocks of code. Objects we define (like our plugins) are blocks of code.
The whole structure of the language is based on blocks of code within braces.
And that's where variables live. For instance, in the HelloWorld plugin, look at
this section where we create and send a message:
public void helloCommand(MessageReceiver caller, String[] parameters) {
String msg = "That'sss a very niccce EVERYTHING you have there..." ;
Canary.instance().getServer().broadcastMessage(msg);
}
In the body of this function we declare and assign a variable named msg . It's
a local variable—it lives only while this particular code block is running,
 
 
 
Search WWH ::




Custom Search