Java and Variable

What is variable?

While a program is executing, it needs the inputs and values. A variable can be considered as a “container” that holds or storage the values while Java’s program is executing.

Type of variable

There are three types or variable in Java including instance, local and static.

Instance variable

A variable is considered as an instance when it was declared outside of method but inside class. For example, myNumber is an instance variable in the following code.

public class className{
    private int myNumber = 10; 
    
    public String methodName(){
    }
}

Local variable

Different with instance variable, the local variable is declared in the method body, and the other methods cannot access that variable. In the following code, we have 2 local variables which are “myLocal” and “myStr” store the values for method “methodName”.

public class className{ 
    public String methodName(){
        int myLocal = 20;
        String myStr = "this is local!";
        return myStr;
    }
}

Static variable

A variable is a static if it declared as static. In the following code, variable “count” is a static variable.

public class className{ 
    static count = 0;
    public String methodName(){
        int myLocal = 20;
        String myStr = "this is local!";
        return myStr;
    }
}

For the static variable, memory allocation happens only once when the class is loaded in the memory. The code below will show how different between static and non-static variable.

class nonStaticVariable{ 
    //the instance variable would be create once Object created   
   int count = 0;  
   nonStaticVariable(){
       count++;
       System.out.println(count);
   }
}  
public class runningClass{
    public static void main(String[] args){
        nonStaticVariable counter1 = new nonStaticVariable();
        nonStaticVariable counter2 = new nonStaticVariable();
        nonStaticVariable counter3 = new nonStaticVariable();
    }
 }

The output would be

1
1
1

Now we change it to use static variable

class staticVariable{ 
   //memory allocation happens only once
   static int count = 0;  
   staticVariable(){
       count++;
       System.out.println(count);
   }
}  
public class runningClass{
    public static void main(String[] args){
        staticVariable counter1 = new staticVariable();
        staticVariable counter2 = new staticVariable();
        staticVariable counter3 = new staticVariable();
    }
 }

The output would be

1
2
3

Conclusion

There are three main types of variable in Java that we need to know at this time. Hoping that my examples gave you some ideas about variable in Java.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: