In the olden days, not only did we have to walk a mile in the chilling winds of a snowstorm to get to school (hey our grandparents had it rougher; they had to do it to get to day care), we also had to make programs without buttons and scrollbars. Now, of course, we have object-oriented programming. This article will introduce you to the most important concepts as they relate to Java.
Since we spoke about variables or fields in the Object portion of our tutorial, we'll cover them more in depth here. Java allows for four types of variables. They are as follows:
Instance Variables are used for storing the individual states of an object.
Class Variable is a single variable that used for all of the objects within a class.
Local Variables are used to store temporary states of an object. A local variable is used only within a method and is not usable outside of where it was declared.
Parameters are always variables, and never fields.
Variable Naming Conventions
Just as you can't run around calling everyone "Hey you!" (well you could, but good luck finding the right Hey You in your phone book), you can't simply tell your computer to use "that variable over there with the funny haircut." Below is a list of rules for naming variables.
Names may consist of letters, numbers, dollar signs, and underscores.
It is better to use the full word when naming variables to avoid confusion later on. For instance instead of naming a button b_g, take the time to spell out bubble_gum. When you get a call at three o'clock in the morning because your program isn't working, you'll thank yourself.
To make the names more readable, if it contains two words, always uppercase the first letter of each word. If it is one word, keep it lowercase. (Example: BubbleGum or bubble). And remember: no spaces.
Variable names are case-sensitive. If you name your variable bubble and refer to it later as Bubble, it will not work.
In the next tutorial on Programming in Java, we will discuss the Data Types of variables (certain variables hold certain types of data) and cover more of the basic language rules.