Java – Variable Types

Variable Types,A variable provides us with named storage that our programs can control. Each variable in Java has a selected type, which determines the dimensions and layout of the variable’s memory; the variety of values that can be saved inside that reminiscence; and the set of operations that may be applied to the variable.

You need to declare all variables earlier than they may be used. Following is the primary form of a variable declaration −

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

Here information type is one of Java’s datatypes and variable is the name of the variable. To declare more than one variable of the desired type, you can use a comma-separated listing.

Following are valid examples of variable announcement and initialization in Java −

Variable Types,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 provide an explanation for various variable types available in Java Language. There are three kinds of variables in Java −

  • Local variables
  • Instance variables
  • Class/Static variables

Variable Types,Local Variables

  • Local variables are declared in strategies, constructors, or blocks.
  • Local variables are created when the technique, constructor or block is entered and the variable may be destroyed as soon as it exits the approach, constructor, or block.
  • Access modifiers can not be used for neighborhood variables.
  • Local variables are seen best inside the declared method, constructor, or block.
  • Local variables are applied at stack level internally.
  • There is no default value for local variables, so nearby variables have to be declared and an preliminary cost must be assigned before the first use.

Example

Here, age is a nearby variable. This is defined interior pupAge() approach and its scope is restrained to only this approach.

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

Following example makes use of age without initializing it, so it might deliver an errors on 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

Variable Types,Instance Variables

  • Instance variables are declared in a class, however outside a way, constructor or any block.
  • When a area is allocated for an object within the heap, a slot for every example variable cost is created.
  • Instance variables are created while an item is created with using the keyword ‘new’ and destroyed while the object is destroyed.
  • variables keep values that ought to be referenced with the aid of a couple of method, constructor or block, or critical components of an item’s nation that must be present for the duration of the elegance.
  • Instance variables may be declared in class stage before or after use.
  • Access modifiers may be given for instance variables.
  • The example variables are seen for all strategies, constructors and block inside the magnificence. Normally, it’s far recommended to make these variables personal (access stage). However, visibility for subclasses may be given for these variables with using get admission to modifiers.
  • Instance variables have default values. For numbers, the default value is zero, for Booleans it’s miles false, and for object references it is null. Values can be assigned all through the announcement or inside the constructor.
  • Instance variables can be accessed immediately by calling the variable call in the elegance. However, within static techniques (when example variables are given accessibility), they must be known as using the absolutely 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 variables also referred to as static variables are declared with the static key-word in a class, however out of doors a method, constructor or a block.
  • There would only be one replica of every magnificence variable according to elegance, no matter what number of items are constituted of it.
  • Static variables are rarely used aside from being declared as constants. Constants are variables which might be declared as public/non-public, very last, and static. Constant variables never change from their initial cost.
  • Static variables are stored inside the static reminiscence. It is rare to apply static variables other than declared very last and used as either public or personal constants.
  • Static variables are created whilst the program starts and destroyed while this system stops.
  • Visibility is similar to instance variables. However, maximum static variables are declared public due to the fact they have to be available for users of the class.
  • Default values are equal as example variables. For numbers, the default cost is zero; for Booleans, it is false; and for item references, it’s miles null. Values may be assigned during the announcement or within the constructor. Additionally, values may be assigned in special static initializer blocks.
  • Static variables may 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 aren’t public and very last, the naming syntax is similar to 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

Note: If the variables are accessed from an outside class, the steady ought to be accessed as Employee.DEPARTMENT