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
- Java variable names are case sensitive. The variable name money is not the same as Money or MONEY.
- Java variable names must start with a letter, or the $ or _ character.
- After the first character in a Java variable name, the name can also contain numbers.
- No space are allowed in the variable declarations.
- Except underscore ( _ ) no special symbol are allowed in the middle of variable declaration
- Variable name always should exist in the left hand side of assignment operators.
- Maximum length of variable is 64 characters.
- 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.
- Variable names are written in lowercase. Example: variable or apple.
- If variable names consist of multiple words, each word after the first word has its first letter written in uppercase. Example: variableName or bigApple.
- Even though it is allowed, you do not normally start a Java variable name with $ or _ .
- 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