Easy Tutorial
❮ Java Arraylist Addall Number Pow ❯

Java Variable Types

In Java, all variables must be declared before they can be used. The basic format for declaring variables is as follows:

type identifier [ = value][, identifier [= value] ...] ;

Format explanation: type is the Java data type. identifier is the variable name. Multiple variables of the same type can be declared using commas.

Here are some examples of variable declarations. Note that some include initialization.

int a, b, c;         // Declares three ints: a, b, c
int d = 3, e = 4, f = 5; // Declares three ints and initializes them
byte z = 22;         // Declares and initializes z
String s = "tutorialpro";  // Declares and initializes the string s
double pi = 3.14159; // Declares a double variable pi
char x = 'x';        // Declares a variable x with the value 'x'

Java supports the following variable types:

Example

public class Variable{
    static int allClicks=0;    // Class variable

    String str="hello world";  // Instance variable

    public void method(){

        int i =0;  // Local variable

    }
}

Java Local Variables

Example 1

In the following example, age is a local variable. It is defined within the pupAge() method and its scope is limited to this method.

package com.tutorialpro.test;

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

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

The compilation and running result of the above example is:

Puppy's age is: 7

Example 2

In the following example, the age variable is not initialized, so it will cause a compilation error:

package com.tutorialpro.test;

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

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

The compilation and running result of the above example is:

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

Instance Variables

Example

Employee.java File Code:

import java.io.*;
public class Employee {
   // This instance variable is visible to subclasses
   public String name;
   // Private variable, visible only within this class
   private double salary;
   // Assigning value to name in the constructor
   public Employee(String empName) {
      name = empName;
   }
   // Setting the value of salary
   public void setSalary(double empSal) {
      salary = empSal;
   }
   // Printing information
   public void printEmp() {
      System.out.println("Name: " + name);
      System.out.println("Salary: " + salary);
   }

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

The above example compiles and runs with the following result:

$ javac Employee.java
$ java Employee
Name: tutorialpro
Salary: 1000.0

Class Variables (Static Variables)

Example:

Employee.java File Code:

import java.io.*;

public class Employee {
    // salary is a static private variable
    private static double salary;
    // DEPARTMENT is a constant
    public static final String DEPARTMENT = "Developer";
    public static void main(String[] args) {
        salary = 10000;
        System.out.println(DEPARTMENT + " Average Salary: " + salary);
    }
}

The above example compiles and runs with the following result:

Developer Average Salary: 10000.0

Note: If other classes want to access this variable, they can do so like this: Employee.DEPARTMENT.

In this chapter, we learned about Java variable types. In the next chapter, we will introduce the use of Java modifiers.

❮ Java Arraylist Addall Number Pow ❯