A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Variables in Java

variables in Java

What is a variable?

Variable is the reserved area in the memory.
There are three different types of variables in java:

  1. Local variabledeclares insides method.

  2. Instance variable declares inside the class, but outside the method is called, it is not declares as "static".

  3. Static variable declares as static, but it can't be local.

Let see an example for more clearance of concept:

/* create a class in the program
class SimpleExampleByHeeraBabu
    /* this is called Instance variable.*/
 int a = 5;   

 /* this is static variable.*/ 
 static int b = 100;   

 Void method ( )
  int c = 9;  /*this is local variable.*/


What are the Variable Types in Java ?

In Java, all variables must be declared before they can be used. The basic form of a variable declaration is shown here:

type identifier [ = value][, identifier [= value] ...] ; The type is one of Java's datatypes. The identifier is the name of the variable. To declare more than one variable of the specified type, use a comma-separated list.

Here are several examples of variable declarations of various types.
Note that some include an initialization. int a, b, c; // declares three ints, a, b, and c.

int d = 3, e, f = 5; // declares three more ints, initializing
// d and f.
byte z = 22; // initializes z.
double pi = 3.14159; // declares an approximation of pi. char x = 'x'; // the variable x has the value 'x'.
This chapter will explain various variable types available in Java Language.

There are three kinds of variables in Java:
  1. Local variables
  2. Instance variables
  3. Class/static variables

Please Continue Reading...