Study Smart, Not Hard. - StudyMuch

Java Variables

Published by StudyMuch on

Java Variables

Java Variables with Programming Examples

In this tutorial you will learn about the “Java Variables” with programming examples, Types of Variables in Java, and you also see programming examples of all the Variables. What is Variable and how many types of Variables in Java. Here we read all the main topic about the Java Variable.

What is Variables?

A variables is a container which holds the value while the java program is executed. A Variable is assigned with a datatype. Variable is a name of memory location.

A Variable is an identifier that denotes a storage location use to store a data value. Unlike constants that remain unchanged during the execution of java program. A variable may take different values at the different time during the execution of the program.

A Variable name can be chosen by the programmer as the meaning way.

Variable is name of reserved area allocated in memory. In other words, it is a name of memory location. It is a combination of “vary + able” that means its value can be change.

Example of Variable;

public class tutorial {
public static void main (String [] args)
   int a = 10;
   float b = 1.5F;
   String c = “Shubham”;
//Here a, b, and c is Variable.

you can see above programming we take the variable in data type “int, float, and string”, a, b and c is a Variable in above programming.

Java Variables StudyMuch

Types of Variables in Java

Mainly there are three types of variables in java;

  • Local Variable
  • Instance Variable
  • Static Variable

Local Variable

A variable declared inside the body of the method is called local variable. You can use this variable only within that method and the other methods in the class aren’t even aware that the variable exists.

A local variable cannot be defined with “static” keyword.

Instance Variable

A variable declared inside the class but outside the body of the method, is called instance variable. It is not declared as static.

It is called instance variable because its value is instance specific and is not shared among instances.

Static variable

A variable which is declared as static is called static variable. It cannot be local. You can create a single copy of static variable and share among all the instances of the class. Memory allocation for static variable happens only once when the class is loaded in the memory.

Now you see below, all the programming examples are available related Java Variable, we written here different – different types of examples, so that you can understand better.

Example of all variable in one programming examples;

public class example {
  int data = 50; //instance variable
  static int b = 100; //static variable
void method () {
    int n = 40; //local variable

Widening: Java Variable Programming Example;

public class example {
public static void main (String [ ] args){
      int a =20;
      float f = a;





Narrowing: Java Variable Example;

public class example {
public static void main (String [] args){
     float f = 10.7f;
     int a = (int)f;





Adding Lower Type: Java Variable Example;

public class example {
public static void main (String [] args){
      byte a =50;
      byte b =50;
      byte c = (byte)(a+b);




So, in this tutorial (Java Variable) you learned about the Java Variables, it’s definition, types of variables with programming examples, I hope you guys, you read this tutorial better and learn something new. If you have any doubt you can ask in the comment section.

Read Also


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *