Incremental Java
Declaring Variables in a Java Program

Review of a Simple Java Class

Let's review what a Java program looks like so far. We've learned a little about variables, but variables, by themselves, aren't Java programs.

Here's the basic Java program. It is called a class definition.

public class Rectangle
{ // START of class definition body
     // class definition BODY goes here
} // END of class definition body
Recall that public class Rectangle is called a class definition header, while everything between the open and close brace is called the class definition body.

Anything to do with a class goes between these two braces. All we have is comments which is ignored by the Java compiler. The comments are for you or other human readers to read.

Methods

A class usually contains methods. At this point, you don't that much about what a method is.

One special method is called main(). (To make it easier to indicate that some name is a method, I put parentheses after the name of the method. Thus, main() is a method because it has parentheses at the end).

Let's put main() into the class definition.

public class Rectangle
{
   public static void main( String [] args )
   { // START of main method body

   } // END of main method body
}
A class definition can have many methods. We just have one.

public static void main( String [] args ) is the method header (also called a method signature). It has its own open and close brace. These indicate the start and end of a method body.

Where to Put Variables

There are two places to put variables.

Local Variables

We can put it in a method body.
public class Rectangle
{
   public static void main( String [] args )
   { 
      int num ;
   } 
}
We declared num in the method body of main().

Variables declared inside a method are called local variables. Local variables can only be used within the method body.

Let's see an example:

public class Rectangle
{
   public static void main( String [] args )
   { 
      int num ;
   } 
   
   public static void foo()
   {
      // Can't use num here
   }
}
In this example, I've written two methods. In addition to main(), I've also written foo(). num is a local variable declared in main() and can only be used in main()'s method body. You can't use it in foo()'s method body.

The method body (i.e., the open and close brace) defines the scope where you can use a local variable.

Since each method body is isolated from each other, then you can declare num in more than one method.

public class Rectangle
{
   public static void main( String [] args )
   { 
      int num ;
   } 
   
   public static void foo()
   {
      // NOT the same num as in main()
      int num ;
   }
}
Each method declares num, but they are different variables.

To give an analogy, suppose there is an unusual college. In this college, each classroom must not have two people with the same name. If Samir is in a classroom, then he must be the only Samir in that classroom.

However, there can be one Samir in one classroom, and a different Samir in a different classroom. That's fine. Since Samir can't be at two different places at the same time, these must be two different Samirs.

In this case, think of main() and foo() as two classrooms. The variable names are like students. Each method can have as many variables as needed, provided all of the names are different. However, a variable name can appear in main() and it can also appear in foo(), but those are different variables, despite the same name.

Instance Variables

There's a second place you can declare variables. You can put it outside any method, but within the class definition body.
public class Rectangle
{
   // Instance variables
   private int width, height ; 

   public static void main( String [] args )
   { 
      int num ;
   } 
}
For now, ignore private. In fact, it doesn't have to be there, but we'll use it later on.

We've declared width and height in the class definition body, but not in any method body. So, it must be an instance variable.

What's an instance variable? More confusion. At this point, it's a variable that's different from a local variable. For now, don't worry about it, because you'll learn about instance variables soon enough.

Instance Variables Can Be Used in Any Method in a Class

OK, here's one place the variables are different. If you declare num inside the main() method body, it can only be used inside of main(). You can't use num from main() (directly) in foo().

However, instance variables can be used in any method in the class definition. Thus, width and height can be used in any method within the Rectangle class, and it is the same variable in all of those locations. Thus, if we use height in main(), it's the same one in foo().

Well, Almost

OK, we have to explain another difficult concept. main() has the word static in front of it (and so did foo()). If a method has the word static in it, then we can't use instance variables.

If we had removed static, we could use instance variables. The only problem is that main() requires static.

If you're confused, don't worry. We won't deal with instance variables right now. We'll focus more on local variables, until you learn more about instance variables.

Keep Local and Instance Variable Names Different

Java permits local variables and instance variables to have the same name. However, this causes conflicts. Java uses local variables if there is a local variable and an instance variable has the same name. It's better just to make sure you don't name instance and local variables the same.

In any case, we'll use local variables for now.