Java provides different types variables to work with different types of data types such as a number, character, boolean, and so on. A variable is like a container in the memory that holds the data used by the Java program. Other words Java Variables used to store data that changes during the execution of the program. Variables can be declared to store values, such as names, addresses, and salary details. Java is a strongly-typed language which means that any variable or an object created from a class must belong to its type and should store the same type of data.
Variables start with data types following variable name and semicolon at the end of variable to close statement
where, datatype: Is a valid data type in Java.
variableName: Is a valid variable name.
Variable Naming Rules
To declare a variables java provide some conventions and rules to be followed for naming variables .variable name always should be short and meaningful later it will make easy for a programmer to understand that this variable using for what purpose
Some Rules are followed for naming conventions
- Variable names may consist of Unicode letters and digits, underscore ( _ ), and dollar sign ($).
- A variable’s name must begin with a letter, the dollar sign ($), or the underscore character ( _ ). The convention, however, is to always begin your variable names with a letter, not ‘$’ or ‘ _ ’.
- Variable names must not be a keyword or reserved word in Java. Variable names in Java are case-sensitive (for example, the variable names number and Number refer to two different variables).
- If a variable name comprises a single word, the name should be in lowercase (for example, velocity or ratio).
- If the variable name consists of more than one word, the first letter of each subsequent word should be capitalized (for example, employeeNumber and accountBalance).
account#Balance Invalid and contains the illegal character # double Invalid and is a keyword
4short Invalid and the first character is a digit
Assigning Value to a Variable
We can assign the value to a variable using = assignment operator.There is two way assign a value to variable first At the time of declaring a variable and other After the variable declaration.
Different Types of Variables
Java programming language supports three types variables
- Instance variables
- Static variables
- Local variables
Instance variables are declared outside the method bust inside class Instance variables can be easily access in any method. Instance variables are also called fields or attributes variables. Each object created from a class contains its own individual instance variables. In other words, each object will have its own copy of instance variables.
Static variables also known as class variables .static variables declared using static keyword inform the compiler that only one copy of this variable exists irrespective of a number of times the class has been instantiated.
Local variables are declared inside the block methods of the scope. Local variables scope only inside the blocks or methods such kind variables are not access able outside in methods class or in the block.in java, these are not specific keyword used to declare local variables hence its position of declaration makes it local
Scope and Lifetime of Variables
In Java, variables can be declared within a class, method, or within any block. The boundaries of the block, that is, opening and closing curly braces define the scope of variables in Java. A scope determines the visibility of variables to another part of the program. Every time a block is defined, it creates a new scope. Similarly, the lifetime of a variable defines the time period for which the variable exists in a program.
The variables declared within the class can be instance variables or static variables. The instance variables are owned by the objects of the class. Thus, their existence or scope depends on upon the object creation. Similarly, static variables are shared between the objects and exists for the lifetime of a class.