Wednesday 8 August 2018

VARIABLES



A variable is a basic unit of storage, which represents memory location in which value can be
stored.
Rules for variables:
1. They must not begin with a digit
2. It should not be a keyword
3. White space is not allowed
4. Variable names can be of any length

DECLARATION OF VARIABLES
Before using any variable, it must first be declared. A variable declaration specifies the
datatype, the variable name, and optionally the default value for the variable.
Syntax: datatype identifier {=default value}
Ex: byte b;
int age, enrolno
boolean male

GIVING VALUES TO VARIABLES
Once you have declared the type of a variable, you can initialize it with some value.
Syntax: variable name=some value
Ex: int year=20;

SCOPE OF VARIABLES
Java actually has three kinds of variables
Instance variables
Class variables
Local variables
Instance variables are created when the objects are instantiated and therefore they are
associated with the objects. They take different values for each object. Class variables are
similar to instance variables, except their values apply to all the instances of a class rather
than having different values for each object. Local variables are declared and used inside
methods.

Variables :
Variables are also the key to creating programs that can perform calculations.
Type of Variables :
1. class variable
2. Instance variable
3. Local variable
4. Final variable
5. shadowing variable

Class Variable :
A class variable is a variable that any method in a class can access, including static
methods such as main.
When declaring a class variable you have two basic rules to follow:
1. You must place the declarations within the body of the class, but not within any of
the class method.
2. You must include the word static in the declaration. The word static comes before the
variable type.
E.g.: public class helloapp {
static string hellomessage;
public static void main(String[] args) {
hellomessage = “Helloworld!”;
System.out.println(“hellomessage”);
}
}

Instance variable :
An instance variable is similar to a class variable, but doesn’t specify The word static in its
declaration.
• Instance variables are associated with instances of classes.
E.g.:
public class helloapp {
string hellomessage;
public static void main(String[] args) {
hellomessage = “Helloworld!”;
System.out.println(“hellomessage”);
}
}
Error :
Cannot make a static reference to the non-static field hellomessage

Local variable :
A local variable is a variable that’s declared within the body of a method.
• You don’t specify “static” on a declaration for a local variable.
• local variables are not given initial default values. The compiler checks to make sure
that you have assigned a value before you use a local variable.

Final variable (Constants) :
• A final variable, also called a constant.
• It is a variable whose value you can’t change once it’s been initialized.
Final variables are class or instance variables
Shadowing variables :
A shadowing variable is a variable that would otherwise be accessible, but is temporarily
made unavailable because a variable with the same name has been declared in a more
immediate scope.
Scope :
The scope of a variable refers to which parts of a class the variable exists in.

*SYMBOLIC CONSTANTS
symbolic constants are used like macros in C and C++
symbolic names take the same form as variable names. But, they are written in capitals to
visually distinguish them from normal variables. After declaration of symbolic constants they
should not be assigned to any other within the program by using an assignment statement.
A constant can be declared as follows:
Examples:
final int STRENGTH = 100;
final int PASS_MARK = 50;
final float PI = 3.14159;

No comments: