Variable in Java

A variable in Java is a named location in memory that holds a value. Variables in Java are used to store data that can be used throughout a program.

Types of variable in Java

There are three main types of variables in Java:

  • Local variables: These variables are declared within a method, constructor, or block of code. They are only accessible within the scope in which they are defined.
  • Instance variables: These variables are declared within a class but outside any method, constructor, or block of code. They are associated with individual instances (objects) of the class.
  • Static variables: These variables are declared with the static keyword within a class but outside any method, constructor, or block of code. They are associated with the class itself rather than with instances of the class.

Local variable example

public class LocalVariableExample {
   public static void main(String[] args) {
      int age; // local variable
      age = 25;
      System.out.println("My age is: " + age);
   }
}

Output;

My age is: 25

Instance variable example

public class InstanceVariableExample {
   // instance variable
   String name;

   public static void main(String[] args) {
      InstanceVariableExample obj = new InstanceVariableExample();
      System.out.println("My name is: " + obj.name);
   }
}

Output:

My name is: null

Static variable example

public class StaticVariableExample {
   // static variable
   static int count = 0;

   public static void main(String[] args) {
      StaticVariableExample obj1 = new StaticVariableExample();
      obj1.count++;
      StaticVariableExample obj2 = new StaticVariableExample();
      obj2.count++;
      StaticVariableExample obj3 = new StaticVariableExample();
      obj3.count++;
      System.out.println("Total objects created: " + count);
   }
}

Output:

Total objects created: 3

Can you answer why the output is 3?

I would love to hear your answer in the comment section below.

Thank you

Frequently Asked Questions

What is a variable in Java?

A variable in Java is a named storage location used to store data temporarily during program execution.

What are the rules for naming variables in Java?

Variable names must start with a letter, underscore, or dollar sign, followed by letters, digits, underscores, or dollar signs. They are case-sensitive and cannot be Java keywords.

How do I declare a variable in Java?

To declare a variable in Java, specify its data type followed by the variable name, like this:
int myNumber;.

What is the scope of a variable in Java?

The scope of a variable determines where it can be accessed. Variables declared inside a block have local scope, while those declared in a class have class scope.

How do I assign a value to a variable in Java?

You can assign a value to a variable using the assignment operator (=), like this:
myNumber = 42;.

Sharing Is Caring:
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments