Variable is a data name that may be used to store a data value. Variables are created when we assign a value to it.
Example:
int a, b, c;
Here a, b and c is the variable name which hold’s values from integer data type.
Conditions for a variable name:
- A variable names can be a letter, digits and underscore character.
- It must begin with a letter.
- White space is not allowed in a variable name.
- It should not be a keyword.
Valid Variable name:
int count;
float neural_beast;
Invalid Variable name:
float char;
int null group;
String word$;
Assigning values to a variable
(=) operator is used to assign a value to a variable. Eg: (int a = 10;), here 10 is the value assigned to the variable a.
Example to add two values:
class add_num
{
public static void main(String args[])
{
int a, b, c;
a = 10;
b = 20;
c = a + b;
System.out.println("The Sum of the value is:" + c);
}
}
Output:
The Sum of the value is:30
Types of Variables
There are three types of variable, they are.
- Local Variable
- Instance Variable
- Static Variable
Local Variable
A variable declared and defined inside the body of the method or with in a block is called local variable. The range of the local variables exist only within the block. These variables can be accessed only within the block.
Example:
class employee_dt
{
public void employee()
{
int n, num = 10;
n = num + 1;
System.out.println(n);
}
public static void main(String args[])
{
employee_dt emp = new employee_dt();
emp.employee();
}
}
Output:
11
Instance Variables
Instance Variables are non-static variables which are declared inside the class but outside the body of method or a block. These variables are created when an object of class is declared and will destroyed when the object is destroyed. The default value of instance variable is 0. We can access instance variable by creating objects.
Example:
class employee
{
int employee_1;
int employee_2;
int employee_3;
int employee_4;
}
class employee_dt
{
public static void main(String args[])
{
employee emp1 = new employee();
emp1.employee_1 = 1;
emp1.employee_2 = 2;
employee emp2 = new employee();
emp2.employee_3 = 3;
emp2.employee_4 = 4;
System.out.println("The First Employee number is:" + emp1.employee_1);
System.out.println("The Second Employee number is:" + emp1.employee_2);
System.out.println("The Third Employee number is:" + emp2.employee_3);
System.out.println("The Forth Employee number is:" + emp2.employee_4);
}
}
Output:
The First Employee number is:1
The Second Employee number is:2
The Third Employee number is:3
The Forth Employee number is:4
Static Variables
Static Variables are declared using static keyword. To access static variable we want to append the class name with the variable, if we access static variable without the class name the compiler will automatically append the class name.
Example:
class employee
{
public static int employee_1 = 1;
public static String name;
}
class employee_dt
{
public static void main(String args[])
{
employee.name = "Regu";
System.out.println("The Employee number is:" + employee.employee_1 + "\t Employee name is:" + employee.name);
}
}
Output:
The Employee number is:1 Employee name is:Regu
Hey! This is mmy first comment herte so I just wanted tto gie a quick shout out and say
I truly enjoy reading through your articles. Appreciate
it!
Really interesting information, I am sure this post has touched all internet users, its really really pleasant piece of writing
oon building upp new website.
Hi there to every body, it’s myy first pay a quick visit of this website; this blog contains awesome and genuinely excellent
data for visitors.
Hello! I wish to say that this post is awesome,
great written and coje with approximately all important infos.
I’d like to look extra posts like this! 🙂
Wow, amzing bkog layout! How long have yyou been blogging for?
you made blogging look easy. Thee overall look of your web site is excellent,
as well as the content.
I need to to thank you foor this fantastic read!! I definitely enjoyed every
bit of it.I havee got you book-marked to look at new things yyou post…
I enjoy reading through your website. Thanks!
Hello there! Wouldd yoou mind if I shae your blog with mmy twitter group?
There’s a lot of people that I think would really appreciate our content.Please let me
know. Thanks!