Everything you need to know about Variables in Java

Published on Aug 02,2019 2K Views

Everything you need to know about Variables in Java

edureka.co

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

Variables in Java can be of Different Types:

 

Variable Declaration and Initialization

A variable is declared by specifying the following parameters:


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.

 

Types of Variables in Java

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.

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.

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.

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.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial