Variable Description In Java Programs

What is a Variable?

This page includes java programs on various java topics such as control statements, loops, classes & objects, functions, arrays etc. All the programs are tested and provided with the output. If you new to java and want to learn java before trying out these program, then read my Core Java Tutorials.

A variable can be thought of as a container which holds value for you, during the life of a Java program. Every variable is assigned a data type which designates the type and quantity of value it can hold.

In order to use a variable in a program you to need to perform 2 steps

  1. Variable Declaration
  2. Variable Initialization

In this tutorial, you will learn-

Variable Declaration:

To declare a variable, you must specify the data type & give the variable a unique name.

Examples of other Valid Declarations are

Variable Initialization:

To initialize a variable, you must assign it a valid value.

Example of other Valid Initializations are

You can combine variable declaration and initialization.

Example :

Types of variables

In Java, there are three types of variables:

  1. Local Variables
  2. Instance Variables
  3. Static Variables

1) Local Variables

Local Variables are a variable that are declared inside the body of a method.

2) Instance Variables

Instance variables are defined without the STATIC keyword .They are defined Outside a method declaration. They are Object specific and are known as instance variables.

3) Static Variables

Static variables are initialized only once, at the start of the program execution. These variables should be initialized first, before the initialization of any instance variables.

Example: Types of Variables in Java

Data Types in Java

Data types classify the different values to be stored in the variable. In java, there are two types of data types:

Listen to this album and more than 40 million songs with your unlimited streaming plans. Brahms hungarian dance 5 violin. Or listen to our entire catalogue with our high-quality unlimited streaming subscriptions. Buy an album or an individual track. Later Learn more.Listen to over 40 million songs with an unlimited streaming plan.

  1. Primitive Data Types
  2. Non-primitive Data Types

Primitive Data Types

Primitive Data Types are predefined and available within the Java language. Primitive values do not share state with other primitive values.

There are 8 primitive types: byte, short, int, long, char, float, double, and boolean Integer data types

Floating Data Type

Textual Data Type

Logical

Java Data Types
Data Type Default Value Default size
byte 0 1 byte
short 0 2 bytes
int 0 4 bytes
long 0L 8 bytes
float 0.0f 4 bytes
double 0.0d 8 bytes
boolean false 1 bit
char 'u0000' 2 bytes

Points to Remember:

  • All numeric data types are signed(+/-).
  • The size of data types remain the same on all platforms (standardized)
  • char data type in Java is 2 bytes because it uses UNICODE character set. By virtue of it, Java supports internationalization. UNICODE is a character set which covers all known scripts and language in the world

Java Variable Type Conversion & Type Casting

A variable of one type can receive the value of another type. Here there are 2 cases -

Case 1) Variable of smaller capacity is be assigned to another variable of bigger capacity.

This process is Automatic, and non-explicit is known as Conversion

Case 2) Variable of larger capacity is be assigned to another variable of smaller capacity

In such cases, you have to explicitly specify the type cast operator. This process is known as Type Casting.
In case, you do not specify a type cast operator; the compiler gives an error. Since this rule is enforced by the compiler, it makes the programmer aware that the conversion he is about to do may cause some loss in data and prevents accidental losses.
Example: To Understand Type Casting
Step 1) Copy the following code into an editor.

Step 2) Save, Compile & Run the code.

Output:

The static can be:

Let's look at static variables and static methods first.

What is Static Variable in Java?

Static variable in Java is variable which belongs to the class and initialized only once at the start of the execution.

  • It is a variable which belongs to the class and not to object(instance)
  • Static variables are initialized only once, at the start of the execution. These variables will be initialized first, before the initialization of any instance variables
  • A single copy to be shared by all instances of the class
  • A static variable can be accessed directly by the class name and doesn’t need any object

Syntax :

What is Static Method in Java?

Static method in Java is a method which belongs to the class and not to the object. A static method can access only static data.

  • It is a method which belongs to the class and not to the object(instance)
  • A static method can access only static data. It can not access non-static data (instance variables)
  • A static method can call only other static methods and can not call a non-static method from it.
  • A static method can be accessed directly by the class name and doesn’t need any object
  • A static method cannot refer to 'this' or 'super' keywords in anyway

Syntax :

Note: main method is static, since it must be accessible for an application to run, before any instantiation takes place.

Lets learn the nuances of the static keywords by doing some excercises!
Example: How to call static variables & methods
Step 1) Copy the following code into a editor

Step 2) Save & Compile the code. Run the code as, java Demo.
Step 3) Expected output show below


Following diagram shows, how reference variables & objects are created and static variables are accessed by the different instances.


Step 4) It is possible to access a static variable from outside the class using the syntax ClassName.Variable_Name. Uncomment line # 7 & 8 . Save , Compile & Run . Observe the output. Step 5) Uncomment line 25,26 & 27 . Save , Compile & Run.

Step 6) Error = ? This is because it is not possible to access instance variable 'a' from java static class method 'increment'.

Java Static Block

The static block is a block of statement inside a Java class that will be executed when a class is first loaded into the JVM

A static block helps to initialize the static data members, just like constructors help to initialize instance members

Following program is the example of java static block.

Example: How to access static block

you will get following output of the program.