Variables in Java

Welcome back to the Java tutorials. In the last session we focused on different types of data types available in Java. We briefly discussed both the primitive data types as well as non-primitive data types.

Coming back to the current tutorial, today we would discuss about the various types of variables that a user can create in Java. We would talk about the local variables, class variables and instance variables. We would also look at their practical implementations.

Our classes are usually made up data members and the methods to manipulate them. Variables are used as identifiers to store the data which can be exploited and manipulated using methods. As we discussed the various data types in our last tutorial, so each of the variable belongs to a specific data type and that is how we get to know the memory allocated, size, type and the bunch of the operations that can be performed on the data that can be stored in the variable.


To be able to perform any operation on the data, we must declare and initialize them using variables. Hence from the above examples, we can come up with the variable declaration and initialization syntax.


data type variable_name = value;

Java provides three types of variables:

  • Local Variables
  • Class Variables
  • Instance Variables

Let us quickly have a look at each one of them.

Local Variables


Note: Compiler doesn’t provide any default value to the local variables. Thus, the user is essentially required to initialize the local variables prior to their usage.



The value of sum is: 27

Class Variables


Default Values for Class Variables:




Shruti’s roll no is: 58

Instance Variables




name is: Shruti

Rollno is: 59

With this, we come to an end of this tutorial. Stay tuned for the upcoming tutorials. We will keep posting more of the useful stuff on Java. Do share your reviews, comment and queries.

Read Similar Posts

Data types in Java

In the last tutorial, we made you acquainted with the basic and suggested conventions used in Java. Many of the conventions may not look like mandatory but are indeed important as and when we move ahead and the code gets more complicated.

In the current tutorial, we intend to discuss about various data types in Java.

We are sure that each one of us at some point of time would have stumbled upon our classic “Hello World” program.

Output:Hello, World

Let us just drift our focus towards the following line of code:


When we talk about classes and objects, they are all about data members and methods. Members are essentially used to store the data as in the above case, we have stored “Hello, World” in the message variable. Data members are also commonly known as identifiers, data variables or simply as variables.

These variables are responsible to obtain the memory chunk for the values to be stored.

How much memory is allocated? Who allocates the memory?

The amount of memory allocated is determined by the type of data you wish to store i.e. the datatype of the variable and the memory is allocated by the Operating system of the machine. Thus, using datatypes, we are leveraged to store different kinds of values like Boolean values, numeric values, string values etc.

What are the different kinds of datatypes in Java?

There are principally two kinds of datatypes available in Java:

  • Primitive type
  • Non – Primitive type

Though we have mainly two kinds of datatypes but one can certainly find number of variants of these two. Let us further discuss them in detail.


As discussed earlier that data types determines the type and size of the variable and the kinds of operations it can undergo, let us discuss more on it.

Primitive Data Types

Java supports eights kinds of different data types. Primitive data types are the one’s which are already defined in the language and associated with a keyword. Let us discuss each of them individually:

Data types


Default Values

Consider a situation where you declare a variable but do not assign any value to it. It is indeed important to know what happens to these kinds of variables. Variables which are declared but not assigned any value will automatically be assigned by the compiler depending on their datatype. These values are known as default values. Most of the times these values are “zero or null”.

Check out the default values below:


Non Primitive Data Types

Non primitive data types generally have three categories. Non primitive data types are also known as Reference types or reference variables. Reference variable are used to access the objects. User can create reference variables using constructors of classes. Reference variables can necessarily represent only one class type and the type cannot be further changed.

Default value of reference variables is null.

Syntax – ClassName RefObj = new ClassName();

With this, we come to an end of this tutorial. Stay tuned for the upcoming tutorials. We will keep posting more of the useful stuff on Java. Do share your reviews, comment and queries.

Read Similar Posts

1 2 3 8