Java Reference
In-Depth Information
Stack vs. Heap
You may have noticed that sometimes we create and directly assign a simple value
to a variable, like inta=25 , and sometimes we use the new keyword to create an object,
like a plugin.
These two kinds of objects are stored differently. Immediate values like integers and
floats are kept on Java's list of function calls that you're making. That list is called
a stack . It's like a stack of pancakes. Each new function call throws a new pancake
on the stack, and when it's done you remove that pancake and you're back to the
previous one. When a function is finished, its “pancake” is thrown away, and any of
its local variables disappear.
But objects that you create with new are kept off in a big pile of memory we call the
heap . They can stick around after your function is gone, if you want them to (like a
plugin does).
You just need to keep a variable somewhere that points to your object; that variable
can be local and passed around, or global. Java keeps track of how many different
variables reference the object created with new . Once no one is using that object
anymore, it gets tossed in the trash. And then when the system feels like it, it empties
the trash and your object is gone. (Java even calls that “garbage collection.”) And as
we mentioned toward the end of the last chapter, you need to be careful: assignment
gives you two references to the same object; using new creates a new copy of an object.
you can instead make it private , like this:
public class WizardingWorld extends EZPlugin {
...
private void makeSuperUberHighWizard(Player p) {
...
}
// Only functions here can call makeSuperUberHighWizard()
}
In general, if you're making a function that you're using internally within this
plugin, and other plugins shouldn't call directly, then make it private . Variables
you are using should almost always be private.
If you want other plugins to see and use it, then make it public .
We'll start using private for our helper functions now.
Plugin: NamedSigns
Let's put a couple of these ideas together and make a plugin that uses helper
functions (with and without return values), private functions and variables,
some low-level array access, and a hash that will store names and locations.
 
 
 
Search WWH ::




Custom Search