The Best Technical Blog Ever

twitterrss

Variables

A variable is used to provide us some named storage(labels - name of the reserved area allocated in memory) that can be manipulated by our programs. They defines a particular position in memory and connect it with a data type. Each variable has a specific data type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. Variables must be defined before they are used.


Syntax: dataType variableName;
Example: int a;

It specifies the integer data type, and reserves memory for it. To declare more than one variable of the specified type, you can use a comma-separated list.


Example: int a,b,c;

Variable Initialization

It gives an initial value into the defined memory . The initial value must be of the correct data type.


Syntax: dataType variableName = initialValue;
Example1: int a=5;
Example2: int a=5, b=2,c=9;

some key points for giving variable name:
  • Variable name cannot include the space character.
  • Do not begin with a digit.
  • Variable name can be of any realistic length.
  • Upper and lower case characters are treated as different characters.
  • A name cannot be a reserved word (keyword).
  • A name must not previously be utilized in the current block of the program.

Variable Types:

In Java, three types of variables are available. They are:

  1. Local variables
  2. Instance variables
  3. Class/static variables

Local variables:

Variables declared in Methods, constructors, or blocks are called Local variable. Local Variables don’t have default value, hence local variables needs to be initialized before it can be used. They are created when the method, constructor or block is entered and these variable will be destroyed once it exits the method, constructor or block. Access modifiers cannot be used for local variables. These variables are visible only within the declared method block.


Example3:
variable

Here, local variable sum is defined inside findsum() method and its scope is limited to this method only.


Note:

If this variable is used without initializing it, it would give an error at the time of compilation like "variable number might not have been initialized"


Instance variables:

Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed. They are used to store object's states. Variables which are defined without STATIC keyword and Outside any method declaration are object specific and known as Instance variable. They are declared in a class, but outside a method, constructor or any block. When a space is allocated for an object in the heap, a slot for each instance variable value is created. Instance variables hold values that must be referenced by more than one method, constructor or block. Access modifiers can be given for instance variables. These variables are visible for all methods, constructors and block in the class. Inside the class, Instance variables can be accessed directly by calling the variable name . However, within static methods and different class should be called using the fully qualified name (ObjectReference.VariableName)


Instance variables also have default values. For numbers the default value is 0, for Booleans it is false and for object references it is null. Values can be assigned during the declaration or within the constructor.


Example4:
variable

Note :

Only FINAL modifier can be applied on local variable. Local variable needs to initialize before they can be used. But it is not necessary for Static or Instance variable.


Class/static variables:

A variable that is declared with the static keyword in a class is called static variable. It cannot be local. But it is declared outside of a method, constructor or a block. Static variables are stored in static memory. All objects of the same class share a single copy of static variables. They are created when the program starts and destroyed when the program stops. Default values are same as instance variables.


Static variables are initialized when class is loaded. But static variables in a class are initialized before any object of that class can be created or any static method of the class runs. Static variables are usually used as constants. Constant variables never change from their initial value. Static variables can be accessed in two ways 1) using object of the class 2)using class name ClassName.VariableName.


Example5:
variable


Note:

If the static final variables are accessed from an outside class, the constant should be accessed as Sample.DEPARTMENT


Static final variables

static final variables are constants. This constant variable name should be in upper case but you can use underscore(_) between it.


Example6:
public class StaticVar
{
   public static final int STATIC_VAR=27;
}

				

  • Here STATIC_VAR is public which means any class can use it. It is a static variable so you won't need any object of class in order to access it. It is final so this variable can never be changed in this or in any class.
  • final variable always needs initialization, if you don't initialize it would throw a compilation error. have a look at below example.

Example7:
public class StaticVar
{
   public static final int STATIC_VAR;
}

				

Output: Error: variable STATIC_VAR might not have been initialized
Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status