The idea of variables is critical to grasping the Java language. In a program, variables are responsible for storing all of the data. To store data, a variable must be declared and initialized.
How to Create a Variable in Java?
There are two steps required in creating variables in Java.
1. Declare the variable
You must declare its type and name. For example:
// declaring variable "x"
int x;
2. Initialize the variable
To store data, you need to save the data to the declared variable. For example:
// initializing variable "x"
x = 10;
Declare and Initialize in One Line
Variable declaration and initialization can also be done on one line. For example:
// declaring and initializing "x" to the value 10
int x = 10;
Types of Variables in Java
There are various types of variables in Java. Below, you will explore each of them.
Local variables
When a variable is declared and initialized within a method, it is a local variable. Local variables exist within the curly braces of a method and can not be "seen" by any other class members. This is called "variable scope". There is no special way to determine a local variable besides its location.
Method Parameters
Similar to local variables, parameters only exist within the method they are declared. Another way to think about it is that a method gets a copy of whatever value is passed to it and stores that value in the parameter variable for use within the method. If the value of a parameter needs to be used elsewhere in a program, a method must return the parameter's value. You'll come back to discussing methods shortly, but here's an example:
// in the method signature below, variables
// "int a" and "int b" are parameters (aka "arguments")
// that will be passed to the multiply() method below
public int multiply (int a, int b){
// return the result of "a" times "b"
return a * b;
}
Instance Variables
Instance variables are declared outside of methods and exist as members of a class. Each instance of a class (you'll come back to this) gets its own instance variables. Instance variables are also visible to other members (variables and methods) within the same class and other classes, depending on access modifiers (public, private, protected).
For example, the following class has two instance variables: name and age.
class Person {
String name;
int age;
}
Static Variables
Static variables are also visible to other members; however, not each instance of a class has its own copy. Static variables are shared by all instances of a class. You'll dig deeper into the concepts of static variables and methods a bit later.
class Person {
String name;
int age;
// the variable "numPersonsBorn" is static - it is a single variable that will be shared
// across all instances of the Person class - more to come on this shortly
static int numPersonsBorn;
}
Now, you can start storing information in your code that can be used later; congrats! Without this, almost no program could exist.
Summary: What are Java Variables?
- Variables are how you store data in Java
- Variables are created by declaration and initialization
Types of Variables
- Local
- Method parameters
- Instance variables
- Static variables