Thursday, August 3, 2017

Variables

Variable is an identifier which can be used to identify input data in a program.

Syntax
Variable_name = value;


Rules to write a Variable Name
  1.     Java variable names are case sensitive. The variable name money is not the same as Money or MONEY.
  2.     Java variable names must start with a letter, or the $ or _ character.
  3.     After the first character in a Java variable name, the name can also contain numbers. 
  4.     No space are allowed in the variable declarations.
  5.     Except underscore ( _ ) no special symbol are allowed in the middle of variable declaration
  6.     Variable name always should exist in the left hand side of assignment operators.
  7.     Maximum length of variable is 64 characters.
  8.     Variable names cannot be equal to reserved key words in Java. For instance, the words int or for are reserved words in Java. Therefore you cannot name your variables int or for.
Java variable naming conventions:
These conventions are not necessary to follow. But it is a good practice to write a java program.
  1.     Variable names are written in lowercase.   Example: variable or apple.
  2.      If variable names consist of multiple words, each word after the first word has its first letter written in uppercase. Example: variableName or bigApple.
  3.      Even though it is allowed, you do not normally start a Java variable name with $ or _ .
  4.      Static final fields (constants) are named in all uppercase, typically using an _ to separate the words in the name. Example: EXCHANGE_RATE or COEFFICIENT.

Variable Types
                                                           





 1. Local variable

A variable which is declared inside the method is called local variable.

2. Instance variable

A variable which is declared inside the class but outside the method, is called instance variable .

 It is not declared as static.

3. Static variable

A variable that is declared within the class with static keyword but outside of method, constructor, or block is known as Static/class variable.

Static variables are accessed by ClassName.VariableName


Example to understand the types of variables

class A
{  
int data=50;//instance variable  
static int m=100;//static variable  
void method()
{  
int n=90;//local variable  
}  
}//end of class  

No comments:

Post a Comment