24 Jun Java Variables
In this lesson, we will learn how to work with Java Variables. Variables in Java are a reserved area allocated in memory. It is a container which holds value. Each variable is assigned a type, which is known as data type. We will also work around the types of Java Variables, including Local, Instance and Class Variable.
The reserved area set for the variable stRank as shown below. Here, the variable is assigned a value 5. Also, you can see, we need a datatype to declare a variable. The int data type is used below.
Declare Variables in Java
As in the above figure, variables are declared by adding a data type, with the variable name.
Here are some examples:
1 2 3 4 5 6 |
int stRank: int score; byte a; boolean result; |
Initialize Variables in Java
To initialize a variable, assign a value to it. Here are some examples:
1 2 3 4 5 6 |
int stRank = 5; int score = 90; byte a = 10; boolean result = true; |
Here’s the usage of variable in a Java program:
For example,
1 2 3 4 5 6 7 8 9 |
public class Studyopedia { public static void main(String[] args) { int score = 90; } } |
The value of the integer variable above is assigned 90.
Variable Types
Here are the types of variables in Java:
Local Variables
A local variable is assigned a value locally. It is a variable that gets declared inside a method and can be accessed inside the same method only. For example,
1 2 3 |
int rank = 10; |
Let’s see the complete example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class StudyopediaDemo { int id = 5; // instance variable static int fees; // static variable void collegeDetails() { int rank = 10; // local variable System.out.println("College Id: " + id); System.out.println("College Rank: " + rank); } public static void main(String args[]) { StudyopediaDemo st = new StudyopediaDemo(); fees = 3000; System.out.println("College Monthly Fees: " + fees); st.collegeDetails(); } } |
Instance Variables
A variable which is declared in a class, but outside the method, is known as instance variable. It is not declared as static.
For example, int id =5;
Let’s see the complete example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class StudyopediaDemo { int id = 5; // instance variable static int fees; // static variable void collegeDetails() { int rank = 10; // local variable System.out.println("College Id: " + id); System.out.println("College Rank: " + rank); } public static void main(String args[]) { StudyopediaDemo st = new StudyopediaDemo(); fees = 3000; System.out.println("College Monthly Fees: " + fees); st.collegeDetails(); } } |
Class/ Static Variables
A class variable is declared static. Class variable is also known as static variable. For example,
1 2 3 |
static int fees; |
Let’s see the complete example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class StudyopediaDemo { int id = 5; // instance variable static int fees; // static variable void collegeDetails() { int rank = 10; // local variable System.out.println("College Id: " + id); System.out.println("College Rank: " + rank); } public static void main(String args[]) { StudyopediaDemo st = new StudyopediaDemo(); fees = 3000; System.out.println("College Monthly Fees: " + fees); st.collegeDetails(); } } |
Here’s the output for the above program, which shows the usage of Java variables.
Note: Learn how to setup Java on windows
Run the above program in the same way and you will get the following output:
No Comments