Learn Java Basics
About Lesson

Variable In Java

A variable is a named storage location in the computer’s memory, where data can be stored and manipulated. It has a data type and a name and can be assigned a value.

Java supports several types of variables, including:

  1. Primitive types: These are the basic data types, such as int, float, double, char, boolean, etc. They store a single value and occupy a fixed amount of memory.

  2. Reference types: These are more complex data types, such as arrays, objects, and classes. They store a reference to an object or array, rather than the actual data itself.

To declare a variable in Java, you use the following syntax:

datatype variableName;

For example, to declare an integer variable named “age”, you would use:

int age;

To assign a value to the variable, you use the following syntax:

variableName = value;

For example, to assign the value 25 to the “age” variable, you would use:

age = 25;

You can also combine variable declaration and assignment into a single statement, like this:

datatype variableName = value;

For example:

int age = 25;

Java also supports variable scope, which is part of the program where the variable can be accessed. Variables can be declared at various levels of scope, such as global, class, method, or block scope. The scope of a variable determines where it can be accessed and how long it remains in memory.

Rules to Declare a Variable

  1. A variable name can consist of Capital letters A-Z, lowercase letters a-z digits 0-9, and two special characters such as _ underscore and $ dollar sign.
  2. The first character must not be a digit.
  3. Blank spaces cannot be used in variable names.
  4. Java keywords cannot be used as variable names.
  5. Variable names are case-sensitive.
  6. There is no limit on the length of a variable name but by convention, it should be between 4 to 15 chars.
  7. Variable names always should exist on the left-hand side of assignment operators.

Types of Variables

There are three types of variables in Java:

  • local variable
  • instance variable
  • static variable

Local Variable

A local variable in Java is a variable that is declared within a method, constructor, or block of code. Local variables are only accessible within the scope in which they are defined, and they are not visible outside of that scope. Once the method, constructor, or block of code in which the variable is declared is exited, the variable is destroyed and its memory is released.

Here are some important points to note about local variables in Java:

  • Local variables must be declared and initialized before they can be used. If a local variable is used before it is declared, a compilation error will occur.
  • Local variables can only be accessed within the method, constructor, or block of code in which they are declared.
  • Local variables are not accessible outside of their scope, which means that they cannot be accessed by other methods or classes.
  • Local variables can have the same name as instance variables or other local variables in the same scope. When this happens, the local variable takes precedence over the instance variable or other local variable with the same name.
  • Local variables are not assigned any default values by Java, which means that they must be explicitly initialized before they are used.

Here’s an example of how to declare and use a local variable in Java:

Here’s an example of how to declare and use a local variable in Java:


public void exampleMethod()
{
int x = 10; // declaring and initializing a local variable
System.out.println(x); // output: 10
}

In the example above, we declared a local variable named “x” inside the “exampleMethod()” method. We initialized it with the value 10 and then printed it to the console using the “System.out.println()” method. Once the method is exited, the local variable “x” is destroyed and its memory is released.

Instance Variable

An instance variable in Java is a variable that belongs to an instance of a class, and it is declared inside the class but outside of any methods. Instance variables are sometimes referred to as member variables or non-static fields. Each instance of the class has its copy of the instance variables, which means that each object can have different values for these variables.

Here are some important points to note about instance variables in Java:

  • Instance variables are declared inside a class but outside of any methods, constructors, or static blocks.
  • Each instance of the class has its copy of the instance variables.
  • Instance variables are initialized when an object of the class is created, and they retain their values until the object is destroyed.
  • Instance variables can be accessed and modified by any method or constructor of the class, as well as by methods of other classes if the instance variable is declared as public.
  • Instance variables are assigned default values if they are not explicitly initialized. The default value depends on the type of the variable. For example, the default value for an int is 0, and the default value for a boolean is false.

Here’s an example of how to declare and use an instance variable in Java:


public class ExampleClass
{
int x; // declaring an instance variable

public void setX(int newValue)

{
x = newValue; // assigning a new value to the instance variable
}

public void printX()
{
System.out.println(x); // printing the value of the instance variable
}
}
// create an object of ExampleClass and set the value of x
ExampleClass obj = new ExampleClass();
obj.setX(10);

// print the value of x

obj.printX(); // output: 10

In the example above, we declared an instance variable named “x” inside the “ExampleClass” class. We then created an object of the class and set the value of “x” using the “setX()” method. Finally, we printed the value of “x” using the “printX()” method. Since “x” is an instance variable, each object of the “ExampleClass” class has its copy of “x”, and the value of “x” for each object can be set and retrieved independently.

Static variable

A static variable in Java is a variable that belongs to a class, and it is declared with the “static” keyword inside the class but outside of any methods. Static variables are sometimes referred to as class variables because they are associated with the class itself, rather than with any particular instance of the class. There is only one copy of a static variable per class, which means that all objects of the class share the same value for the variable.

Here are some important points to note about static variables in Java:

  • Static variables are declared inside a class but outside of any methods, constructors, or static blocks, and they are declared with the “static” keyword.
  • There is only one copy of a static variable per class, which means that all objects of the class share the same value for the variable.
  • Static variables are initialized when the class is loaded by the JVM, and they retain their values until the program terminates.
  • Static variables can be accessed and modified by any method or constructor of the class, as well as by methods of other classes if the static variable is declared as public.
  • Static variables are assigned default values if they are not explicitly initialized. The default value depends on the type of the variable. For example, the default value for an int is 0, and the default value for a boolean is false.

Here’s an example of how to declare and use a static variable in Java:


public class ExampleClass
{
static int x; // declaring a static variable

public void setX(int newValue)

{
x = newValue; // assigning a new value to the static variable
}

public void printx()
{
System.out.println(x); // printing the value of the static variable
}
}
// set the value of x
ExampleClass.x = 10;

// create an object of ExampleClass and print the value of x

ExampleClass obj = new ExampleClass();
obj.printX(); // output: 10
// set the value of x using the object and print it again
obj.setX(20);
ExampleClass obj2 = new ExampleClass();
obj2.printX(); // output: 20

In the example above, we declared a static variable named “x” inside the “ExampleClass” class. We then set the value of “x” directly using the class name, and printed the value of “x” using an object of the class. Since “x” is a static variable, all objects of the “ExampleClass” class share the same value for “x”. We then set the value of “x” using an object of the class, and printed the value of “x” again using a different object of the class. The value of “x” is still 20 because all objects of the class share the same value for “x”.

Scope of Variables In Java

The scope of a variable refers to the part of the program where the variable is accessible and can be used. The scope of a variable is determined by where the variable is declared. There are three types of variables in Java based on their scope: local variables, instance variables, and static variables.

  • Local variables have a limited scope and are accessible only within the method, constructor, or block where they are declared. Once the method, constructor, or block ends, the local variable is destroyed and its value is lost. Local variables are declared inside a method, constructor, or block using the variable’s data type, followed by the variable’s name.

Here is an example of a local variable:


public void exampleMethod()
{
int x = 5; // x is a local variable
System.out.println(x); // output: 5
}

In this example, the variable “x” is declared inside the method “exampleMethod”, so its scope is limited to that method. The variable “x” can only be accessed and used within the method “exampleMethod”. Once the method ends, the variable “x” is destroyed and its value is lost.

  • Instance variables have a wider scope and are accessible throughout the class in which they are declared. Instance variables are declared inside a class but outside of any methods, constructors, or static blocks. Each instance of the class has its own copy of the instance variables, which means that each object can have different values for these variables.

Here is an example of an instance variable:


public class ExampleClass
{
int x; // x is an instance variable

public void exampleMethod()

{
System.out.println(x); // accessing the instance variable x
}
}

In this example, the variable “x” is declared inside the class “ExampleClass”, so its scope is the entire class. The variable “x” can be accessed and used by any method of the class, as well as by methods of other classes if the instance variable is declared as public.

  • Static variables have the widest scope and are accessible throughout the class and all its instances. Static variables are declared with the “static” keyword inside the class but outside of any methods, constructors, or static blocks. There is only one copy of a static variable per class, which means that all objects of the class share the same value for the variable.

Here is an example of a static variable:


public class ExampleClass
{
static int x; // x is a static variable

public void exampleMethod()

{
System.out.println(x); // accessing the static variable x
}
}

In this example, the variable “x” is declared with the “static” keyword, so its scope is the entire class and all its instances. The variable “x” can be accessed and used by any method of the class, as well as by methods of other classes if the static variable is declared as public.

It is important to note that local variables can have the same name as an instance or static variables in a class. In such cases, the local variable takes precedence over the instance or static variable within the scope of the local variable.

Join the conversation