Java Lesson 4 - Variables
In this tutorial, we will learn these topics - what variables are, how to use variables in Java, and the types of variables.
What is a variable:
As the name suggests, a variable is whose value can vary from time to time. For an example, we are given 45, 87 written inside two boxes on a paper using a pencil, and we are asked to swap them:
In order to swap them, first we remember one of the numbers and erase it:
Then erase the other number and re write in other box:
Now we write the number we remembered in the empty box:
Every time we create a variable in a computer program, the computer program creates a space in our computer memory (RAM). Then stores our value in it and keeps the reference to that memory location. Whenever we recall our variable value, the computer program goes to the memory location of our variable using the reference and grabs the stored value for us.
A variable can have different types of values. As per our example, a variable should be able to hold Integer values. There are other types of values such as Floating Point values and Character values (String values).
Java variable syntax:
Following syntax demonstrates how to declare a variable in Java:
[data_type] [variable_name] = [variable_value];[data_type]- refer to type of information stored in memory area. int for Integer values, float for Floating Point values. We will discuss more about data types in next tutorial.[variable_name]- refer to name of variable. For an example myTemporaryValue[variable_value]- refer to the value to be stored in memory area.
int myVariable = 10;
float myFloatVariable1 = 45.345F;
String myStringVariable = "Hello Java";As an example lets run following Java code:
public class MyClass {
public static void main(String[] args) {
System.out.println("--------------------------------");
int number1 = 10;
int number2 = 20;
System.out.println("Number 1 = " + number1);
System.out.println("Number 2 = " + number2);
int sum = number1 + number2;
System.out.println("Sum = " + sum);
System.out.println("--------------------------------");
float floatNumber1 = 22.35F;
float floatNumber2 = 53.23F;
System.out.println("Float Number 1 = " + floatNumber1);
System.out.println("Float Number 2 = " + floatNumber2);
float floatSum = floatNumber1 + floatNumber2;
System.out.println("Float Sum = " + floatSum);
System.out.println("--------------------------------");
String stringValue = "Hello Java";
System.out.println("String Value = " + stringValue);
System.out.println("--------------------------------");
}
}
We will get the following output for this code:
Lets have a closer look at our Java code. We have declared number1 and number2 variables as int variables. We can print them on our console using println. We can do arithmetic operations on number1 and number2 variables, because they are Integer values. We can create another Integer variable called sum and assign the result of number1 and number2 addition as value of sum variable. We have declared Floating Point variables called floatNumber1 and floatNumber2 using float data type and we can perform arithmetic operations on them. Next we have declared a String variable. We can assign a set of characters to String variable and use println to display it on console. We will get familiar with these variables when we are progressing through our tutorial series.
Types of variables in Java:
class House {
String wallColor = "White";
String wallMaterial = "Brick";
String roofColor = "Brown";
}public class MyClass {
public static void main(String[] args) {
House house1 = new House();
System.out.println("House 1 : Wall Color -> " + house1.wallColor + " | Wall Material -> " + house1.wallMaterial + " | Roof Color -> " + house1.roofColor);
House house2 = new House();
house2.wallColor = "Red";
house2.roofColor = "Brown";
System.out.println("House 2 : Wall Color -> " + house2.wallColor + " | Wall Material -> " + house2.wallMaterial + " | Roof Color -> " + house2.roofColor);
House house3 = new House();
house3.wallColor = "Light Yellow";
house3.wallMaterial = "Wood";
house3.roofColor = "Red";
System.out.println("House 3 : Wall Color -> " + house3.wallColor + " | Wall Material -> " + house3.wallMaterial + " | Roof Color -> " + house3.roofColor);
}
}javac MyClass.java and run it using java MyClass, we can see we have created three different House objects and they are assigned with different properties. But we used the same House class to create our objects.1. Static Variables:
Static variables are also known as class variables. A static variable is a variable that declared with a static modifier. It means that there is exactly one copy of defined variable can exist, regardless of how many times the class has been instantiated.
class Student {
static String name = "Smith";
}public class MyClass {
public static void main(String[] args) {
System.out.println("--------------------------------------------");
System.out.println("Student Name -> " + Student.name);
System.out.println("--------------------------------------------");
Student student1 = new Student();
System.out.println("Student 1 Name -> " + student1.name);
System.out.println("--------------------------------------------");
Student student2 = new Student();
System.out.println("Student 2 Name -> " + student2.name);
System.out.println("--------------------------------------------");
student1.name = "Adam";
System.out.println("Student Name -> " + Student.name);
System.out.println("Student 1 Name -> " + student1.name);
System.out.println("Student 2 Name -> " + student2.name);
System.out.println("--------------------------------------------");
student2.name = "Colin";
System.out.println("Student Name -> " + Student.name);
System.out.println("Student 1 Name -> " + student1.name);
System.out.println("Student 2 Name -> " + student2.name);
System.out.println("--------------------------------------------");
}
}We will get the output as:
As per the definition of static variables, even we created multiple objects of the Student class and assigned different values to each "name" property of student objects, value of the variable is the same. Because static variables have only one copy regardless of the number of times we instantiated our class.
2. Instance Variables:
Instance variables are declared in class without static modifier. Their values are unique to each instance of class. Instance variables are also known as state variables.
Student.java:
class Student {
String name = "Smith";
}public class MyClass {
public static void main(String[] args) {
System.out.println("--------------------------------------------");
Student student1 = new Student();
System.out.println("Student 1 Name -> " + student1.name);
System.out.println("--------------------------------------------");
Student student2 = new Student();
System.out.println("Student 2 Name -> " + student2.name);
System.out.println("--------------------------------------------");
student1.name = "Adam";
System.out.println("Student 1 Name -> " + student1.name);
System.out.println("Student 2 Name -> " + student2.name);
System.out.println("--------------------------------------------");
student2.name = "Colin";
System.out.println("Student 1 Name -> " + student1.name);
System.out.println("Student 2 Name -> " + student2.name);
System.out.println("--------------------------------------------");
}
}We will get the output as:
We can see that, unlike static variables, each "name" property of student objects has a unique value at the end.
3. Local Variables:
Local variables are temporary variables declared inside methods. They exist until method execution completes. Local variables are only visible to the methods in which they are declared. They are not accessible for rest of the class.
Student.java:
class Student {
String name = "Smith";
void printAge() {
int age = 17;
System.out.println("Age -> " + age);
}
}MyClass.java:
public class MyClass {
public static void main(String[] args) {
Student student = new Student();
System.out.println("Student Name -> " + student.name);
System.out.println("Student Age -> " + student.age);
}
}When we try to compile MyClass.java, we will get this error stating that compiler cannot find "age" in Student object. Because age local variable is inside printAge() method and it cannot be accessed outside of method scope. But inside of printAge() method we can access age variable and we can display its value.
MyClass.java
public class MyClass {
public static void main(String[] args) {
Student student = new Student();
System.out.println("Student Name -> " + student.name);
student.printAge();
}
}Now we will get the output as:
4. Method Arguments:
An argument variable is a variable that is passed to a method when the method is called. Arguments are also only accessible inside the method that declares them.
Student.java
class Student {
String name = "Smith";
void printAge(int age) {
System.out.println("Age -> " + age);
}
}MyClass.java
public class MyClass {
public static void main(String[] args) {
Student student = new Student();
System.out.println("Student Name -> " + student.name);
student.printAge(20);
}
}Now age is an argument of printAge() method. Whenever we call printAge() we need to pass a value for the age. Then age can be used inside printAge() method. Here is the output:
- Instance variable are unique to each instance of a class.
- Class variable can have exactly one copy regardless of how many times the class has been instantiated.
- To access instance variable, we MUST create a new instance of class. But class variables are accessible through class reference without creating new instances.
class Student {
static String name = "Smith";
int age =
}MyClass.java:
public class MyClass {
public static void main(String[] args) {
System.out.println("Student Name -> " + Student.name);
Student student = new Student();
System.out.println("Student age -> " + student.age);
}
}Here we do not need to create a student object to access static name variable but we need to create a student object to access age instance variable.
- Java variable names are case sensitive. student, Student and STUDENT are different to each other.
- Java variable names must start with a letter, or the $ or the _ character (student, $student, _student).
- After the first character in a variable name, the name can contain letters, numbers, $ or _ characters.
- Java variable names cannot be reserved keywords in Java. int, float, break, continue, etc cannot be a variable name.










Comments
Post a Comment