Variables in Java

0
69
Spread the love
  • 13
  • 35
  • 12
  • 18
  •  
  •  
  •  
  • 11
  •  
  •  
    89
    Shares

Variable in Java
Variable in Java

Variables in Java:

Variables in Java are user defined tokens or identifier in java programming that denotes a storage location used to store data value.When a constant remains unchanged during the execution,a variable can take different values(including an instance). So variables are place holder and changes its value over time.Variables are pointers to an object.Simply declaring a variable does not create storage for the variable.This post also talks about variables in java rules.

Value is the thing that is stored in a variable.Value should be of type variable. When a variable declaration is executed,memory is allocated for the variable.Also these memory needs to contain some definite value before the variable can be used as an expression.

In java there are five types of variables available for usage.They follow the Access Specification. Details of Access Specification is given here.The area of the program where the variable is accessible is called its scope.

Variables in Java name can be chosen by the coder in a meaningful way so that it can reflect what it represents in the program.In an assignment statement,the thing is being assigned always on the left side of the equal(=) sign and the value is right side.

Computer can only understand binary(0/1)So we need to convey the computer what is the meaning of sequence of binary. This gives the concept of type. A type of a variable is the range of values that the variable is capable to store.

The name space rules applies to packages,classes and members within classes.

What happens when we define the variable?

  1. It tells the compiler what is the variable name.
  2. It specifies what type of data the variable will hold.
  3. It also defines the scope of the variable.
  4. It also tells the compiler how much memory it has to reserve to operate on this variable.

The general rule to define a variable as follows

  1. It may consist of alphabets,digits ,underscore and dollar.
  2. They can not begin with a digit.
  3. Uppercase and lowercase are distinct so Age,age and AGE are there different variables.
  4. It should not be a reserved keyword.
  5. White space inside a variable name is not allowed.
  6. Variable length can be of any length but we need to choose one which is lengthy as elaborate the purpose and short to read and understand.

General form of the variable declaration:


type variableName// for single variable
type variableName1,variableName2,variableName3...variableNameN //for multiple variables
 

Assignment of a variable

Variables in Java must be given a value after it has been declared and before it is used in an execution. This can be done in following ways:

  1. By using assignment expression (assign values during decleration) like int a=2;or int x=y=z=3;
  2. By using a read statement
  3. By dividing the expression in two parts like int a;a=2 ;

Scope of a variable

The concept of the lifetime of a variable occupies a significant portion of the programming effort. The question are..

  • How long the variable lasts?
  • When  shall we destroy the variables?
confusion over the lifetime of the variable can lead to many defects.However, in this paragraph, I tried to explain very basic rules of the scope of a variable.
Scope determines both the visibility and lifetime of the variables defined within the scope.In java scope is determined by the curly braces {}

//first block
{
int x=0;
int y=0;
//x,y are available
  {
//second block 
   int m=0;
   int n=0;
//x,y,m,n are available
  }
//only x,y are avaialble
  {
//third block
   int p=0;
   int q=0;
//x,y,p,q are available
  }
}
//x,y are out of scope
 

A variable defined within the scope is available only to the end of that scope.Java being a free form language , the extra spaces,tabs and carriage return do not affect results of the code.If we have variable of same type we can use same line to declare them.


int age,rollNumber,noOfPens;
 

If we have variable of different types we need to declare separately.


int age,rollNumber;
float salary;
 

we can give variables their starting values during declaration:


int age=10;
float Salary=1000.43;
 

Variables can be initialized once at starting but can be assigned many times.

 

Classification of the variables

The classification of Variable is as follows:

  1. Instance Variable
  2. Class Variable
  3. Local variables
  4. Parameters

Instance Variable

Instance variables also a variables in Java are data elements or object references that can change it’s value.An object variable is called fields in java.These variables are declared at the beginning of the class definition.Instance variables are mostly private in nature and non static. Objects store their individual state in the Instance variable,so their values are unique to the object.Instance variables must be declared before they are used.Instance variables are created when objects are instantiated and they are associated with the object.
Instance variables are initialized in the following 4 ways:
  • Instance variables can be declared and initialized in the same statement.
Like:variables in java example

private String firstName="abc";
//or
private String firstName; //declare the variable and allocate some memory space for it.
firstName="abc"; //initial value is assigned
 

it can also be an expression too.And it is legal to initialize several variables in one declaration statement.


int x,y=1;
int n=3,m=n+1;
//this feature is common in loop too
int i=5;
for(i=5;i<10;i++){}
  • In practice, Instance variables are often  initialized by a constructor call. variables in java example

Class MyClass{
private String firstName;
public MyClass()
{
this.firstName="abc";
}
public MyClass(String firstName)
{
this.firstName=firstName;
}
}
 
  • There is also a third possibility to use a separate public method to initialize them:variables in java example

Class MyClass{
private String firstName;
public void setFirstName(String firstName)
{
this.firstName=firstName;
}
}
 
  • Also if it a public member then creating an instance of the class and instance.variable_name

We use instance variable when we want the fields in an object to have unique value.This is as per Object specification.The set of values of an object’s instance variable is known as it’s state. Thus Instance variables can be initialized and subsequently modified so that the state of the object can be same or different from other objects of the same class.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 13
  • 35
  • 12
  • 18
  •  
  •  
  •  
  • 11
  •  
  •  
    89
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here