Java Identifiers Lesson
Java Identifiers Lesson
double n;
n=10;
System.out.println(n);
}
}
Example 2:Declare and initialize at the same
time
package java101;
double n =10;
System.out.println(n);
}
}
Declaring multiple variables
• This refers to declaring more than one variables. You can use two
approaches.
1. Declare all the variables in the same line without initializing and then
initialize later.
2. Declare each variable in its own line and initialize them later
3. Declare each variable in its own line initialize them immediately.
Example:
Think of a program that is supposed to subtract discount from price and
output totalcost
Ideally here we have three variables namely (discount, price, totalcost)
Example using approach 1
package java101;
public class Java101 {
public static void main(String[] args) {
double price;
double total_cost;
double discount;
price=1000;
discount=50;
total_cost=price-discount;
System.out.println(total_cost);
}
}
Example using approach 3
package java101;
public class Java101 {
double price=1000;
double discount=50;
double total_cost=price-discount;
System.out.println(total_cost);
}
}
Types of Variables in Java
• Local Variables
• A variable defined within a block or method or constructor is called a local
variable.
• These variables are created when the block is entered, or the function is
called and destroyed after exiting from the block or when the call returns
from the function.
• The scope of these variables exists only within the block in which the
variable is declared. i.e., we can access these variables only within that
block.
• Initialization of the local variable is mandatory before using it in the
defined scope.
Types of Variables in Java
• Instance Variables
• Instance variables are non-static variables and are declared in a class
outside any method, constructor, or block.
• As instance variables are declared in a class, these variables are created
when an object of the class is created and destroyed when the object is
destroyed.
• Unlike local variables, we may use access specifiers, for instance, variables.
If we do not specify any access specifier, then the default access specifier
will be used.
• Initialization of Instance Variable is not Mandatory. Its default value is 0
• Instance Variable can be accessed only by creating objects.
Types of Variables in Java
• Static Variables
• Static variables are also known as Class variables.
• These variables are declared similarly as instance variables. The difference is that static variables
are declared using the static keyword within a class outside any method constructor or block.
• Unlike instance variables, we can only have one copy of a static variable per class irrespective of
how many objects we create.
• Static variables are created at the start of program execution and destroyed automatically when
execution ends.
• Initialization of Static Variable is not Mandatory. Its default value is 0
• If we access the static variable like the Instance variable (through an object), the interpreter will
show the warning message, which won’t halt the program. The interpreter will replace the object
name with the class name automatically.
• If we access the static variable without the class name, the interpreter will automatically append
the class name.
Parameter Variables
• Variables that are passed to methods, constructors, or blocks. They
represent the values given to the method or constructor when called.
• They are accessible only within the method or constructor in which
they are declared.
• They exist only during the execution of the method or constructor.