Saturday, August 2, 2008

Creating a Java Variable: Two Steps

There are two steps to creating a variable; declaration and initialization.

Declaration is creating a name and saying what type of variable it names:
int count;
String name;
Scanner input;
JFrame frame;

Here we're saying "I hereby declare that I have a variable named count and that it will be used to name an integer variable. I also declare that a variable called name will be used to refer to a String object." And so on.

Now, these variables are only half done at this point. We've created names, but we haven't actually associated them with anything yet. It's like telling someone the names of your pets before you actually get them.

"I have a hamster named Wilmot and a cat called Tiger and a dog named Bowser."

"Where are they?"

"I don't have them yet."

At this point, telling someone to feed Wilmot wouldn't make much sense. You need a pet to go with the name. Similarly, asking your program to fetch data from your Scanner named input doesn't make sense until you have attached it to an actual instance of a Scanner. So let's do it:
input=new Scanner(;

This is called initialization. We gave it an initial value. In this case, we created a new Scanner object and made input refer to it (using the = sign.)

We can initialize the rest, too:
frame=new JFrame("Hello.");

We initialized count to 0, name to an empty string, and frame to a new Jframe object.

Now we have things associated with the names. Now if we tell our program to get data from input and set name to point to it, it'll be able to do so, because now there's an actual Scanner connected with the name input:;

Shortcut:Two Steps in One

You can declare and initialize a variable in one statement. But you still have to do both.*
int count=0;
String name="";
Scanner input=new Scanner(;
JFrame frame=new JFrame("Hello.");

This looks confusing. It's the redundancy of seeing, for example, Scanner twice on the same line. This happens because the object name, Scanner, is the same as the constructor name for making a new object of that type, Scanner(), except that the constructor has parentheses after it (marking it as a method, and the capitalized name being the name of a class tells you that it's a constructor method that makes a new one of those objects.)

We don't have to create a new object for initialization. If there's already an object of that type available, we can initialize a newly declared variable to that same object:
Scanner keyboard=input;

This declares a new Scanner called keyboard and initializes it to point to the same Scanner as input. In pet terms, this is like giving a nickname to your hamster Wilmot. You may have two names, say, Wilmot and Fluffy, but you've still got one hamster--he just goes by either name.
Hamster wilmot=new Hamster("Syrian", AGOUTI);
Hamster fluffy=wilmot;

If you want another object, you need to create one:
Hamster fred=new Hamster("Teddy Bear", BANDED);
Now you have a second hamster called fred.

Further reading.

*Actually, Java doesn't strictly require initialization all the time. But it's good programming practice to do so. It prevents a lot of bugs and nasty surprises in your programs.