Skip to content

Java Variables Declaration | Types – Scope with Examples

  • by

A Java Variables is a container which store (hold) the value of unit like Strings, Integers, Boolean, etc. Any Variables are assigned to its data type. Where In Java 2 types of data type – primitive and non-primitive.

So if Variables are a store or hold the value of then there is should be memory involved, when you give a varible name it means a memory location, and data type help to what kind of memory location it is.

Java Variables Declaration Types - Scope with Examples

How to Use Java Variables?

There is 2 step to use of Variables.

  1. Java Variables declaration
  2. Java Variable Initialization
How to do Variables declaration and Initialization in java

Variables declaration

A Specify the data type and give the variables name is called variables declaration. Here is an example of a Valid (Righ way) Variables Declarations and code.

int count;

float pi;

String name;

char word;

Variable Initialization

Assing the value to a variable is called Variable Initialization. See the below example an int variable direct assigns the value. Where used “f” String used the double quote ” ” and char used sing Quotes’ ‘.So different types of data types assign it to value different manners.

int count = 100;

float pi = 3.14f;

String name = "EyeHunts";

char word = 'c';

Java Variables types & Scope

In Java, there are three types of variables:

  1. Local Variables
  2. Instance Variables
  3. Static Variables
Examples of All types Variables in Java static, Instance and local

Local Variables

A Variables which declared inside the body of methods are called Local Variables, it’s scope only inside Method.

Instance Variables

Defined Outside a method and Inside the class (class level) declaration are called Instance Variables. And this variable is not the static variable (with the Static keyword).

The scope of instance variables is determined by the access specifier (modifier – public, protected, private, and default) that is applied to these variables.

Static Variables

A Variable is used static keyword is called Static variables.

It initialized only once, at the start of the program. These variables should be initialized first, before any instance variables

Examples of All types Variables in Java

class ClassName {
    int count = 100; //instance variable  

    static int a = 0; //static variable  

    void method() {
        int b = 77; //local variable  
    }
}

Java Variables Declaring name rules

Here are some rule of declaring variables name in java, you must be remembered.

  • All variable names must begin with a letter of the alphabet, an underscore, or ( _ ), or a dollar sign ($).  The convention is to always use a letter of the alphabet.  The dollar sign and the underscore are discouraged.
  • No spaces or special characters are allowed.
  • Can use ALL uppercase letters but it’s primarily used for constant variables.
  • Don’t use a java keyword or a reserved word for a variable name.

Do comment if you have any doubt and suggestion on this tutorial.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java Variables Examples are in Java 11, so it may change its different from Java 9 or 10 or upgraded versions.

Leave a Reply

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