Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Variables are the basic requirements in any program be it Java, Python or JavaScript. It is the basic unit of storage. It acts as a container and is used to hold data values. The values held by the variable can be changed during the execution of the program. Every variable is assigned a data type. Variable, in simpler terms, is a name given to a memory location. So I have lined up the docket for Variables in Java in the following order:
Variables in Java can be of Different Types:
A variable is declared by specifying the following parameters:
Datatype: The type of data that is stored in the variable.
Variable name: The unique name given to the variable.
Value: The initial value stored in the variable.
int age = 50 ; float weight = 50.60;
In the above example, int is the data type, age is the name given to the variable, and 50 is the value. Similarly, float is the data type, weight is the variable name and 50.60 is the value.
Java provides the user with three types of variables:
Local Variables: These types of Variables are defined within a block, a method, or a constructor of the program.
These variables are created when the function is called and are destroyed immediately after the function call is returned.
Local variables prohibit the use of access modifiers.
These variables can be accessed only within the particular block.
public class EmployeeId { public void EmployeeId() { // local variable ID int id = 0; id = id + 6; System.out.println("Employee ID : " + id); } public static void main(String args[]) { EmployeeId obj = new EmployeeId(); obj.EmployeeId(); } }
Output:
Employee ID : 6
In the example given above, the variable id is local to the function, i.e. it can be used only within that function.
On using the local variable outside the scope, an error is returned. Let’s have a look at the following code to understand it better.
public class EmployeeId { public void EmployeeId() { // local variable id int id = 0; id = id + 6; } public static void main(String args[]) { // using local variable outside the scope System.out.println("Employee ID : " + id); } }
Output:
/EmployeeId.java:12: error: cannot find symbol System.out.println("Employee ID : " + id); ^ symbol: variable id location: class EmployeeId 1 error
Instance Variable: They are variables that can be declared in a class, outside a block, a method, or a constructor. They are non-static.
These variables are created when an object of a class is created and destroyed when the object is destroyed.
Access Modifiers can be used for instance variables.
When no modifier is specified, the default modifier is used.
Instance Variables have default values, 0 for numbers, false for Boolean, and null for object references.
import java.io.*; class Price { // Instance variables that are declared in a class and not inside any function int guitarPrice; int pianoPrice; int flutePrice; } public class Main { public static void main(String args[]) { // first object Price ob1 = new Price(); ob1.guitarPrice = 10000; ob1.pianoPrice = 5000; ob1.flutePrice = 1000; // second object Price ob2 = new Price(); ob2.guitarPrice = 9000; ob2.pianoPrice = 4000; ob2.flutePrice = 2000; // displaying the price for first object System.out.println("Price for first object:"); System.out.println(ob1.guitarPrice); System.out.println(ob1.pianoPrice); System.out.println(ob1.flutePrice); // displaying the price for second object System.out.println("Price for second object:"); System.out.println(ob2.guitarPrice); System.out.println(ob2.pianoPrice); System.out.println(ob2.flutePrice); } }
Output:
Price for first object: 10000 5000 1000 Price for second object: 9000 4000 2000
Static Variables: They are similar in nature to Instance Variables. The major difference is that they are declared using the static keyword and only a single copy of a static variable per class is allowed.
They are also known as Class Variables.
Only a single copy of a static variable per class is allowed, irrespective of the number of objects created.
These variables are created at the start of the program and are automatically destroyed when the execution of the program is completed.
The default values of the static variables are the same as the instance variables.
To access static variables, creating an object of that class is not necessary.
The variable can be accessed by:
class_name.variable_name
c import java.io.*; class Manager { // static variable salary public static double salary; public static String name = "Jonathan"; } public class Main { public static void main(String args[]) { // accessing static variable without object Manager.salary = 90000; System.out.println(Manager.name + "'s avg salary:" + Manager.salary); } }
Output:
Jonathan's avg salary:90000.0
The variables discussed above are widely used for efficient programming experience. Each variable has its own unique property and must be used appropriately.
With this, we come to an end of these Variables in Java article. I hope the above-mentioned examples were enough for you to get started in Java, check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “Variables in Java” article and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 28th September,2024 28th September SAT&SUN (Weekend Batch) | View Details |
edureka.co