# Variables in Java

Variable is a name of reversed memory area of any values. Variable’s value can be change during execution of program.Variable is for representing data of a specific type. In other words java variable store information which is needed to execute your program. It can be name , user Id etc. and as per
logic you can change it.

### Variable declaration in java

To use a variable we need declare variable first . You declare it by telling the compiler its name as well as what type of data it can hold. The variable declaration tells the compiler to allocate appropriate memory space for variable based on its data type. To declare the variable in Java, we can use following syntax as

``datatype variableName;``

Here, datatype is which type value store by variable which can any like: int, float etc. and variableName can be any like salary , name etc. Now we see How do you create a variable in java below example

``````public class JavaVaraible{
public static void main(String[] args) {
int salary ;
}
}``````
Here , we have declare variable salary of int type . It means salary variable can hold int type value.

### Initialize variables in java

Now we see how we can initialize a variable in java.  We will declare and initialize a variable with value 10 as following program

``````public class JavaVaraible{
public static void main(String[] args) {
int salary =10;
salay= salary+ 1000;
System.out.println(" salary ="+salary);
}
}``````

In this program we store salary in a variable 1000 and it will become 2000 when we added 1000 to it.We can print variable in java (read variable in java ) by using it’s name here .In details read a variable we will see given below examples.

### Types of variables in java

There are three type variables in Java
1. Local variable
2. Instance variable
3. Static variable

### 1. Local Variable

Local Variable in java have following properties

• Variable declare inside method is called local variable.
• These variable only access inside the specific method/block/constructor.
• Access modifiers cannot be used for local variables.

We will create local variable as following example

``````publicclassLocalVariable {

publicstaticvoidmain(String[] args) {
LocalVariable localVariable = newLocalVariable();
localVariable.display();
}
publicvoiddisplay(){
inti = 10;// Local Varaible
System.out.println(" i ="+i);
}
}``````
Output:
``i =10``

### 2. Instance Variable :

Instance variable in java have following properties

• Variable declare inside the class but outside method is called instance variable.
• Instance variable belongs to object , not to class.
• Access modifiers can be used for instance variables.

Now we see examples of instance variable in java.

``````publicclassInstanceVariable {
int i=20;
public static void main(String[] args) {
InstanceVariableinstanceVariable = newInstanceVariable();
System.out.println(" instance variable ="+ instanceVariable.i);
}
}``````
Output :
``instance variable = 20``

### 3. Static Variable

Static variable have following properties in java

• Variable declare inside class with static keyword is called static variable.
• Static variable can not local.
• Static variable belongs to class ,not to object.
• Access modifiers can be used for static variables.
• Static variable access as ClassName.StaticVariableName.

Let’s see this by code

``````publicclassStaticVariable {

static int i=100;

public static void main(String[] args) {
System.out.println("price of bag is ="+ i);
}
}``````
Output :
``price of bag is =100``

### Java Variable Naming Conventions

There are a few rules for naming conventions of variables as following

• Java variable names are case sensitive. The variable name salary is not the same as Salary or SALARY.
• Java Variable names must begin with a letter (A to Z or a to z), currency character (\$) or an underscore (_).
• After the first character, identifiers can have any combination of characters.
• A reserved keyword cannot be used as variable.
• Examples of legal identifiers: name ,\$salary, _MAX, __5_value.
• Examples of illegal identifiers: 123abc, -salary.

We can follow Variable naming convention as All instance, static and method parameter variable names should be in camel case notation. They should be short and enough to describe their purpose In Program. Temporary variables can be a single character e.g. the counter in the loops.

References :

A Guide To Java

Java Doc – Variable

## One comment

1. Unknown says:

what is reference variable and what are relation between reference variable and object,can you please clarify it giving memory diagram.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7534372,"math_prob":0.870921,"size":4382,"snap":"2021-43-2021-49","text_gpt3_token_len":918,"char_repetition_ratio":0.19255368,"word_repetition_ratio":0.054597702,"special_character_ratio":0.21976267,"punctuation_ratio":0.11910995,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9616647,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-21T10:52:47Z\",\"WARC-Record-ID\":\"<urn:uuid:477e6320-54a7-443a-9ee7-76bed363ce27>\",\"Content-Length\":\"37576\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:536476b8-dc22-4bbe-88cc-86b44476d886>\",\"WARC-Concurrent-To\":\"<urn:uuid:0730584c-e96d-4963-a4c2-fc802b4eaacc>\",\"WARC-IP-Address\":\"92.204.171.50\",\"WARC-Target-URI\":\"https://www.javavogue.com/2015/11/variables-in-java/\",\"WARC-Payload-Digest\":\"sha1:2XLAKM5Q7Z6LJEUJECAC57P3IYHJNR3W\",\"WARC-Block-Digest\":\"sha1:VYDZTIEW3PWW2NS5IT6JJFZCQ3TFBDO4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585405.74_warc_CC-MAIN-20211021102435-20211021132435-00372.warc.gz\"}"}