Java – Variable Types

Java – Variable types, declaration and initialization

Variable Types- The variable is provided to us by the storage name so that our program can be manipulated. Every variable in Java has a specific type that determines its size and placement in memory; the range of values that can be stored in memory; and a set of operations that can be applied to a variable.

Java - Variable types

All variables must be declared before using them. Below is the main ad form:

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

To declare more than one variable of the specified type, you can use a comma-delimited list.

The following are examples of variable declaration and initialization in Java:

int a, b, c; // Declare three integers a, b, and c.
int a = 10, b = 10; // An example of initialization.
byte b = 22; // Initialize a byte variable b.
double pi = 3.14159; // Declare and assign pi.
char a = 'a'; // A variable a of type char is assigned the value 'a'.

In this tutorial, we’ll take a look at the different types of variables available in the Java language. There are three types of variables :

  • local variables;
  • instance variables;
  • static variables or class variables.

Local variables in Java

  • Local variables are declared in methods, constructors, or blocks.
  • Local variables are created when a method, constructor, or block is run and destroyed after the method, constructor, or block has completed.
  • Access modifiers cannot be used for local variables.
  • They are visible only within a declared method, constructor, or block.
  • Local variables are implemented at the stack level internally.
  • There is no default value for local variables in Java , so they must be declared and an initial value must be assigned before first use.

Example

“Age” is a local variable, it is defined inside the “pupAge ()” method and its scope is limited only by this method.

public class Test {
    public void pupAge () {
       int age = 0;
       age = age + 7;
       System.out.println ("Puppy age:" + age);
    }
   
    public static void main (String args []) {
       Test test = new Test ();
       test.pupAge ();
    }
}

You will get the following output:

Puppy age: 7

Example without initialization

An example of using “age” without initialization. The program will throw an error at compile time.

public class Test {
    public void pupAge () {
       int age;
       age = age + 7;
       System.out.println ("Puppy age:" + age);
    }
   
    public static void main (String args []) {
       Test test = new Test ();
       test.pupAge ();
    }
}

This will result in the following compile-time error message:

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

Instance variables

  • Instance variables are declared in the class, but outside of a method, constructor, or any block.
  • When space is allocated for an object on the stack, a slot is created for each value of the instance variable.
  • In Java, instance variables are created when an object is created using the “new” keyword and destroyed when the object is destroyed.
  • Variables contain values that must refer to more than one method, constructor, or block, or major parts of an object’s state that must be present throughout a class.
  • Instance variables can be declared at the class level, before or after use.
  • Access modifiers can be provided for instance variables.
  • Instance variables in Java are visible to all methods, constructors, and blocks in a class. It is generally recommended to make them private (access level). However, you can make them visible to subclasses of these variables using access modifiers.
  • Instance variables have default values. The default is 0 for numbers, false for booleans, and null for object references. Values can be assigned on declaration or in the constructor.
  • Instance variables in Java can be accessed directly by calling the variable name inside the class. However, static methods and various class (when accessibility is given to instance variables) must be called using the fully qualified name – ObjectReference.VariableName .

Example

import java.io. *;
 
public class Employee {
    // Instance variable is public to any child class.
    public String name;
   
    // The salary variable is visible only in Employee.
    private double salary;
   
    // The variable name is assigned in the constructor.
    public Employee (String empName) {
       name = empName;
    }
 
    // The variable salary is assigned a value.
    public void setSalary (double empSal) {
       salary = empSal;
    }
   
    // This method displays employee data.
    public void printEmp () {
       System.out.println ("name:" + name);
       System.out.println ("salary:" + salary);
    }
 
    public static void main (String args []) {
       Employee empOne = new Employee ("Adam");
       empOne.setSalary (1000);
       empOne.printEmp ();
    }
}

The program will produce the following output:

name: Adam
salary: 1000.0

Class Variables or Static Variables in Java

  • Class variables, also known as static variables in Java , that are declared with a static keyword in the class, but outside of a method, constructor, or block.
  • There will only be one copy of every static variable in the class, no matter how many objects are created from it.
  • Static or class variables are rarely used in Java, except when declared as constants. Constants are variables that are declared public / private, final and static. Constants never change from their original value.
  • In Java, static variables are created when a program is started and destroyed when the program stops running.
  • Visibility is like an instance variable. However, most static variables are declared public because they must be available to users of the class.
  • The default values are the same as for instance variables. The default is 0 for numbers, and false for Boolean data; and for object references – null. Values can be assigned on declaration or in the constructor. In addition, they can be assigned in special static initializer blocks.
  • Static variables can be accessed by calling the class name ClassName.VariableName .
  • When declaring class variables as public, static, final, names are in uppercase. If static variables are not, the syntax is the same as for instance and local variables.

Example

import java.io. *;
 
public class Employee {
    // salary (salary) variable private static
    private static double salary;
 
    // DEPARTMENT (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 output:

Development average salary: 1000

Note: to be accessed from an outer class, constants must be available as Employee.DEPARTMENT.