Java: Keywords and Variables
Keywords and Variables
Here, in this page we will discuss about keywords and variables in Java Programming Language. We will discuss each of them in brief.
Keywords
Keywords can be defined as a set of predefined reserved words that have a special meaning for the compiler and cannot be used as classes, methods, variables, or any other identifier. These keywords are 51 in number. out of these 49 are the ones that are used and 2 are those which are not used. These keywords can be seen as follows:List Of Keywords
abstract | assert | boolean | break |
---|---|---|---|
byte | case | catch | char |
class | const | continue | default |
do | double | else | enum |
extends | final | finally | float |
for | go to | if | implements |
import | instance of | int | interface |
long | native | new | package |
private | protected | public | return |
short | static | strictfp | super |
switch | synchronized | this | throw |
throws | transient | try | void |
Variables
A variable is a name given to some memory location. It can be assumed as a container which holds the value while the Java program is executed. A variable is always assigned with a data type.These are of three types:-
Local variable
Instance variable
Static variable
1. Local Variable
- A local variable is the one which is declared with the body of a method.
- It’s scope is limited to the body of that method and the other methods in the class aren’t even aware of the existence of such variable.
- Initial declaration of local variable is compulsory.
- Static variables are declared using the static keyword within a class outside any method constructor or block.
2. Instance Variable
- Instance variables are those which are declared inside the body of a class but not within any methods.
- These differ with each instance of the class created although they have the same identity.
- It is called instance variable because its value is instance specific and is not shared among instances.
- It’s default value is zero.
3. Static Variable
- Static variables are also known as class variable because they are associated with the class and common for all the instances of class.
- We can create a single static variable which can be shared among all the instances of the class.
- Static variables are declared using the static keyword within a class outside any method constructor or block.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment