Variable types in Java

While writing programs, we often need to store values or some pieces of information to compile some services. To store these temporary values, we must use different variables.  And thus, we brought this article will be describing the different types of Java variables and data type Programming.

What are Variables?

We already know that to compile a complete phases of a program compiling, we must store some values—that where variables come. A variable is a memory location that is reserved for the value store. The value depends on the data type. We will explain the data type after this variable section.

However, while writing the program, we must mention the data type after creating the variables. There are three types of variables we find in Java. They are; The local variables, Instance variable, and class/static variable.

Local Variables

The local variables are the variables used within a method, constructor, or block. In an easy word, local variables can perform its job only if its in a block. For example, you are in a for loop or while loop, and you must use the local variables just within this loop. The local variable won’t be visible outside the loop.

Another point to be noted that the local variables don’t have any default values. Therefore, whenever you use the local variable, you must assign it before you move to work with this local variable. After all, if you use this local variable outside the block or didn’t assign any values, your compiler will show you the error. For example,

For (int i=0, i≥10, i++ ) { 
//your code here

Here, “int” is the data type, and “i” is the local variables only works within the loop.

Instance Variables

This type of variable usually is declared in a class variable. But we must declare variables outside a method, constructor, or any blocks. Generally, these types of instance variables are available for the entire class methods, constructors, and code blocks.

We can declare an instance variable right after we create an object for a java class. And then, we can choose the data type right before the variable name. We don’t need to initialize the instance variable as we did for the local variable. For example,

// Java variables and data type example

package main;
public class First {
          // the instance variable is assigned for any child class
          public String website;
          // traffic is visible here for first class
          private double traffic;
          // The website variable is assigned in the constructor.
          public First (String empWeb) {
             website = empWeb;
          // We assigned a traffic value here
          public void settra(double emptra) {
             traffic = emptra;
          // Its a print function
          public void printEmp() {
             System.out.println("website  : " + website );
             System.out.println("traffic :" + traffic);
          public static void main(String args[]) {
             First empOne = new First ("");
Java variables and data type

Class/static variables

We usually declare this type of variable in a class. But we must put it outside a method, constructor, or any block. While writing a static variable, we can do this by putting a “static” keyword before the instance variables.

Once you put a static keyword before an instance variable, it will work until the program is at the end. A static variable starts when the program code starts. It ends when the program comes to an end. For example,

// Java variables and data type example

package main;
public class First{
         static int marks;
         static String subject;
         //These two variables are static variables
         static void disp(){
             System.out.println("Marks: "+marks);
             System.out.println("Subject: "+subject);
         public static void main(String args[])
               marks = 59;
               subject = "Java Programming";
Java variables and data type

This Post Has 2 Comments

Leave a Reply