logo

Java Variable Types


Show

A variable gives us with named storage space that our plans can influence. Each changeable in Java has an exact type, which decides the size and explains the variable's memory; the variety of values that container be stored inside that memory; and the set of processes that can be applied to the changeable.

You must announce entire variables before they can be utilized. Following is the fundamental form of a variable declaration:

data type variable [ = value][, variable [ = value] ...] ;

Here data type is one of Java's datatypes and variable is the name of the variable. To declare more than one variable of the specified type, you can use a comma-separated list.

Following are suitable examples of variable statement and initialization in Java:

Example

int a, b, c;         // Declares three ints, a, b, and c.
int a = 10, b = 10;  // Example of initialization
byte B = 22;         // initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value of PI.
char a = 'a';        // the char variable a iis initialized with value 'a'

This chapter will explain various variable types available in Java Language. There are three kinds of variables in Java −

  • Local variables
  • Instance variables
  • Class/Static variables

Local Variables

Local changeable are created when the technique, constructor, or slab is entered and the changeable will be wipe out once it exits the method, constructor, or block.

  • Access modifiers never are utilized for local variables.
  • Local uneven are observable only inside the announced method, constructor, or block.
  • Local variables are applied at stack level inside.
  • There is no default worth for local changeable, so local variables should be stated and an early value should be allocated before the initial use.

Example

Here, age is a local variable. This is defined inside pupAge() method and its scope is limited to only this method.

public class Test {
   public void pupAge() {
      int age = 0;
      age = age + 7;
      System.out.println("Puppy age is : " + age);
   }

   public static void main(String args[]) {
      Test test = new Test();
      test.pupAge();
   }
}

This will produce the following result −

Output

Puppy age is: 7

Example

The following example uses age without initializing it, so it would give an error at the time of compilation.

public class Test {
   public void pupAge() {
      int age;
      age = age + 7;
      System.out.println("Puppy age is : " + age);
   }

   public static void main(String args[]) {
      Test test = new Test();
      test.pupAge();
   }
}

This will produce the following error while compiling it −

Output

Test.java:4:variable number might not have been initialized
age = age + 7;
         ^
1 error

Instance Variables

  • Instance variables are announced in a rank, but external a process, constructor or any wedge.
  • When a room is assigned for an object in the mound, a slot for each example variable value is generated.
  • Instance inconsistent are generated when an object is generated with the use of the keyword 'new' and destroyed while the object is demolished.
  • Instance changeable hold values that have to be referenced by more than one technique constructor or block, or necessary parts of an object's state that have to be present during the class.
  • Instance patchy can be stated in class level previous to or after use.
  • contact modifiers can be known for example variables.
  • The occurrence variables are noticeable for entire ways, constructors and block in the class. Usually, it is advised to make these changeable private (access level). But, visibility for subclasses can be known for these changeable with the employ of access modifiers.
  • Instance patches have default values. Designed for numbers, the default worth is 0, for Booleans it is false, and for entity orientations it is null. Values can be allocated through the assertion or within the constructor.
  • Instance variables can be admission straight by calling the patchy name within the class. However, within standing methods they should be called with the fully qualified name. ObjectReference.VariableName.

Example

import java.io.*;
public class Employee {

   // this instance variable is visible for any child class.
   public String name;

   // salary  variable is visible in Employee class only.
   private double salary;

   // The name variable is assigned in the constructor.
   public Employee (String empName) {
      name = empName;
   }

   // The salary variable is assigned a value.
   public void setSalary(double empSal) {
      salary = empSal;
   }

   // This method prints the employee details.
   public void printEmp() {
      System.out.println("name  : " + name );
      System.out.println("salary :" + salary);
   }

   public static void main(String args[]) {
      Employee empOne = new Employee("Ransika");
      empOne.setSalary(1000);
      empOne.printEmp();
   }
}

This will produce the following result −

Output

name  : Ransika
salary :1000.0

Class/Static Variables

  • Class capricious also recognized as stationary variables are declared with the static keyword in a class, excluding outside a process, constructor or a block.
  • There would only be one copy of both class capricious per class, despite of how many objects are generated from it.
  • Static changeable are infrequently used other than being stated as constants. Constants are capricious that are affirmed as public/private, last, and static. Constant variables never modify from their first value.
  • Static capricious are stored in the stationary memory. It is uncommon to employ static up-and-downs other than stated final and employed as either public or private constants.
  • Static inconsistent are generated when the program begins and destroyed when the program ends.
  • Visibility is alike to example variables. Yet, most stationary variables are affirmed public because they must be obtainable for users of the class.
  • Default values are similar as case variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor. Additionally, values can be assigned in special static initializer blocks.
  • Static variables can be accessed by calling with the class name ClassName.VariableName.
  • When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final, the naming syntax is the same as instance and local variables.

Example

import java.io.*;
public class Employee {

   // salary  variable is a private static variable
   private static double salary;

   // DEPARTMENT is a constant
   public static final String DEPARTMENT = "Development ";

   public static void main(String args[]) {
      salary = 1000;
      System.out.println(DEPARTMENT + "average salary:" + salary);
   }
}

This will produce the following result −

Output

Development average salary:1000