Variables in Java

Variables are nothing but labels, which are given to a memory location. As variables are written in the human-readable language, so it is easier to access memory addresses using them.

Humans cannot understand the Memory Location/Address. Memory location could have some values inside, which we are not sure of, any/no value could be there.

variables-kotlinIn layman terms, In the above image, I have given some address or a location for each bucket, now for a moment's sake close your eyes and tell the bucket location of the big Kitten. I am not crazy, so please do give a try.

Unless you memorize you cannot guess or remember the locations, am I right ?, Yes.

Now let's provide some unique names to each of the buckets, and let's try to remember/ guess it. In the below image, I have given some names to each bucket and which indirectly points to the bucket location.

variables-location-labelled-kotlin

Now I have labeled the buckets, could you try to guess the big cat (tongue out cat) label. Yes, it is possible as we have to guess the label, and the big cat bucket label is angryCat. And this label represents the bucket location/address.

Let's get back to technical, All the values(kittens) in programming languages are stored in a place called memory(bucket), and this memory is made of series of cells, and each cell has a unique address in your computer.

The addresses of these cells are in a hexadecimal format like 0x7ff9c05f, which we cannot memorize (unless you belong to Albert Einstein's family), so to make it easy we provide some name/label to the address and this name is nothing but variables.

The type of a variable defines the range of values that the variable can hold and the operations that can be done on those values.

The variable name needs to be chosen by the programmer in a meaningful way so that it reflects what it is representing a program.
Variables Naming Convention

There are a few rules and conventions related to the naming of variables.

  • Java variable names are case sensitive. The variable name counter is not the same as Counter or COUNTER.
  • Java variable names must start with a letter, or the $ or _ character.
  • After the first character in a Java variable name, the name can also contain numbers (in addition to letters, the $, and the _ character).
  • Java keywords cannot be used as variable names.
    For instance, the words int or static are reserved words in Java. Therefore you cannot name your variables int or for.
  • Blank spaces cannot be used in variable names.

Variables Stages

All the variables in the Java Programming go under two processes, which are :

  • Declaration
  • Assignment

Declaration

We need to tell the Java programming language what kind of data we are going to assign to a variable then only java can decide what kind of memory it may need and what type of operation to allow on the variable; this is called Declaration.

type variable_name;

type variable_name, variable_name, variable_name;

// variable definition
int    width, height=5;
char   letter='C';
float  age, area;
double d;

Assignment or Initialization :

Initialization means nothing but assigning the value to the variables which were declared. You can only initialize variables that are declared earlier.

Assignment never happens before the Declaration

There could be two kinds of assignments which are

  • Static : Values are assigned during the declaration
  • Dynamic : Values are assigned later on rather than During declaration
// actual initialization
// static
int i = 10;
boolean flag - false;

// dynamic
int width;
width = 10;
age = 26.5;

Types of Variables in java

Java Programming language defines mainly three kinds of variables. These types are based Modifier

  • Static Variables
  • Non-static / Instance variables
  • Local Variables

Variable types based in Declaration Place

In Java, variables are categorized based on the place of the declaration; We have two kinds of variables in Java.

  • Global Variables
  • Local Variables

Local variables

  • Local variables are declared in methods, constructors, or blocks.
  • Local variables are created when the method, constructor, or block is entered, and the variable will be destroyed once it exits the method, constructor, or block.
  • Access modifiers cannot be used for local variables.
  • Local variables are visible only within the declared method, constructor, or block.
  • Local variables are implemented at the stack level internally.
  • There is no default value for local variables, so local variables should be declared, and an initial value should be assigned before the first use.

Global variables

If you have experience with other programming languages, such as C and C++, you have probably used global variables.

A global variable is one that is accessible to all parts of a program and is usually declared as part of the class but ouside other bloks like methods and blocks.

There are two kinds of Global variables

  • Static Variables
  • Instance variables

Static variables

A static variable one of the variable types and it is common to all the instances (or objects) of the class. When the user changes the value of the static variables, it will reflect everywhere and in every object of that class irrespective of which block of code changed it.

In other words, you can say that only a single copy of a static variable is created and shared among all the instances of the class. Memory allocation for such variables only happens once when the class is loaded in the memory.

Every class will be loaded only once per execution/session so that we will have one copy and everybody has to use only that copy

Syntax :

public class Variables {
    static String name = "chercher.tech";
    static int age = 16;
    public static void main(String[] args) {
        System.out.println("The name of my website is :"+name);
        System.out.println("Age(in months) of the Website is :"+age);
    }
}
Points to remember about static variable:
  • static variables are also known as class variables.
  • static means to remain reflective.
  • In Java, static means that it will be the same for all the instances created for that class.
  • static variable need not be called from an object. static variables are called by classname.static variableName

Non Static variables /Instance

Instance variables are the variables which are declared as Global level Variables, but without static Keyword, Instance variables are also called as non-static variables, field, property, object-variables.

Each object has its own copy of each variable, and thus, it doesn't affect the instance variable if one object changes the value of the variable.

We will discuss more on objects in : Classes and Constructor chapter.

class Student
{
 String name;
 int age;
}

name and age are instance variable of Student class

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions