In Java, variables, also known as fields, are used to store data or information(numbers, characters etc.) by allocating a certain area in the memory. You may also Join W3training School Java Training in Gurgaon to learn from java Industry experts.
As the type of data or information may vary, java supports data types which specifies:
- size of the variable being stored in the memory
- actions applicable to the variable, and
- range of values that a variable may take.
A variable can be declared in java programming language in the following ways.
Syntax : modifier datatype variable_name = value ;
- For a single variable: public int age = 24
Here, int is data type, age is the name of the variable and 24 is the value of the variable. - For more than one variable: public int age = 24, height = 170, weight = 72
Here, three variables age, height and weight are used having values 24, 170 and 72 respectively and separated by a comma.
TYPES OF VARIABLES IN JAVA
Java programming language consists of following four types of variables.
- Local Variables
- Instance/ Non-static Variables
- Class/ Static Variables
- Parameters/ Arguments
(1) LOCAL VARIABLES
➽ These are the variables which are declared inside methods and constructors in a class.
➽ There scope is limited and can be accessed only within methods and constructors.
➽ Modifiers(public, private, protected) are absent in local variables and no other keyword is used to identify the local variable.
➽ Local variables do not have default value. It is important to assign some value initially, otherwise, an error will be generated while compiling the program.
Example:
class LocalVariable {
public void johnAge() {
int age = 0; // Initialization of local variable by assigning ‘0’ to it
age = age + 26;
System.out.println(“John’s age is :” + age);
}
public static void main(String args[]) {
LocalVariable obj = new LocalVariable ();
obj.johnAge();
}
}
Output:
John’s age is : 26
(2) INSTANCE/ NON-STATIC VARIABLES
➽ Instance variables are those variables which are declared outside methods and constructors in a class. It means they can be accessed outside methods and constructors in a class.
➽ Instance variables depend on the object. Creating an object will result in the creation of an instance variable and eliminating an object will result in the elimination of the instance variable.
➽ Modifiers(public, private, protected) are present in the instance variables.
➽ Instance variables make use of default values when they are not given an initial value.
Example:
class InstanceVariable {
public String name;
public InstanceVariable(String customerName) {
name = customerName;
}
public void printName() {
System.out.println(“Customer name is :” + name);
}
public static void main(String args[]) {
InstanceVariable obj = new InstanceVariable(“John”);
obj.printName();
}
}
Output:
Customer name is : John
(3) CLASS/ STATIC VARIABLES
➽ Class variables are those variables which are declared outside the methods and constructors with a keyword ‘static’ present between access modifier and the variable name in a class. However, the choice to put an access modifier is entirely dependent on the programmer.
➽ As a program starts, a static variable is created and stored in the static memory and as a program stops, it gets destroyed.
➽ Like Instance variable, it also has access modifiers to deal with the visibility for all methods and constructors.
➽ Their use is limited unless employed as constants(public or private). Only class variables use static keyword, using it with other variables(instance variable, local variable, etc.) will result in a compile-time error.
➽ They also bring into use the default values of data types when the initialization(assigning initial value) is not done.
Example:
class StaticVariable {
public static String school;
public StaticVariable(String schoolName) {
school = schoolName;
}
public void printSchool() {
System.out.println(“School name is :” + school);
}
public static void main(String args[]) {
StaticVariable obj = new StaticVariable(“W3training School”);
obj.printSchool();
}
}
Output:
School name is : W3training School
(4) PARAMETERS/ ARGUMENTS
➽ Parameters are those variables which are declared inside method. They cannot be treated as fields(like local variables).
➽ Modifiers(public, private, static, protected) cannot be used with Parameters.
➽ They exist in the memory while a method is executed and loose their existence as the method returns.
➽ In the method public static void main(String[] args), ‘args’ represents the parameter or argument.
Example:
class Parameters {
public String dog;
public Parameters(String dogBreed) {
dog = dogBreed;
}
public void printBreed() {
System.out.println(“Breed of the dog is :” + dog);
}
public static void main(String args[]) {
Parameters obj = new Parameters(“Great Dane”);
obj.printBreed();
}
}
Output:
Breed of the dog is : Great Dane
Also Check : Data Types in Java Programming Language
We have provided you the best possible description on Types of Variables in Java Programming Language. Hope you like this article. For more updates and related information, stay connected to our blogs on Java Programming Language.