Java Variable Example 4 – Parameters

Java Variable Comparison Chart
The difference between the various variable types in Java we’ve discussed.

Parameters are variables that represent the arguments passed into a method. They are declared in the method declaration. Take the main method as an example:

args is declared in the method declaration for main (inside the parentheses).  Like any declaration, you need to declare its type, an array of instances of the String class. In other words, you declare the class or method, then inside the parentheses you declare what Java is supposed to be looking for.  In this case it’s a set of Strings.

Parameters You’ve already seen examples of parameters, both in the Bicycle class and in the main method of the “Hello World!” application. Recall that the signature for the main method is public static void main(String[] args). Here, the args variable is the parameter to this method. The important thing to remember is that parameters are always classified as “variables” not “fields”. This applies to other parameter-accepting constructs as well (such as constructors and exception handlers) that you’ll learn about later in the tutorial. Java Variables

Makes perfect sense… wait… what? Variables and fields aren’t the same thing? Sigh… okay before we get into parameters let’s clear up that line.

Let’s look at the code from example 3 (I put it above, up yonder). In Java, Non-Static Fields/ Instace Variables are ones like speed and gear. Meanwhile, newGear is a local variable.  The gearShift is our parameter in question.

A field is a variable that is declared inside the class block but outside of the methods or constructors.

Parameters are the names of the variables that are going to be used inside of the method. You pass in arguments, and parameters are used inside of the method.  An argument is also called an actual parameter, because it is the actual value that is being computed while the parameter is the variable itself. Meanwhile, the words arguments and parameters are mostly used interchangeably, because it is often confusing when trying to talk about your methods and it’s usually not too important to distinguish.


Java Variables Examples 3 – Local Variables

Local variables are designated inside some block of code that limits their scope.  That means the variable can only be set, accessed, or retrieved in the scope of the block.  In programming, these variables are only visible and editable in the block of code in which they are created. More specifically in Java:

Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class. Java Variables Tutorial

Let’s look at an example:

In this example, we are in the class Bicycle with the method changeGear which takes input for the gear change (gearShift) and then outputs the new gear.  The new gear is a local variable (newGear) which is can only be used in the method.

Our example is fine for demonstrating a local variable but it’s not the only example of a local variable.   A local variable can be inside constructors, lambda’s, conditional blocks, etc.

Why keep variables local?

  1. Memory. In Java (and most languages) when a variable is local, it doesn’t have to remain in memory. This might increase performance.
  2. Safety. By “encapsulating” a variable, you keep it safe. It’s less likely that  you will accidentally change the value. Java’s encapsulation powers aren’t limited to local fields, private instance variables vary in usage, but follow similar principles.
  3. Ease of use. While points 1 and 2 definitely make coding Java easier, naming variables is really important. Picking the right, descriptive identifier is necessary to keep code legible and workable. Local variables make it easy to name them with simple and descriptive identifiers. Additionally, the type casting makes it easy to see what the variable does locally.

Java Variables Example 2 –Class Variables

Graphic shows class variable in relationship to instance.
Class variables are the same across all instances of a class. They can be changed from anywhere, unless final is invoked.

Unlike instance variables, class variables are the same across the class and all instances of the class. They can be set from anywhere, but they change across the board in all instances. Continue reading “Java Variables Example 2 –Class Variables”

Java Variables Examples 1 – Instance Variables

Instance Variables Flow Chart
What is an instance variable? A class member that each instance of the class inherits, each containing an independent value from the same member in other instances.

Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in “non-static fields”, that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent from the currentSpeed of another.

Referencing Java Certification Tutorial, Section 1-1 Variables

In the following sample code, we set up a class. This class will be referenced in our application. This class has two instance variable members, speed and gear. While each bike shares a variables named speed and gear each instance has its own variable, with its own place in memory and value. Continue reading “Java Variables Examples 1 – Instance Variables”