Variables in Java Programming

In this tutorial we will study what is variables in java and what’s are the type of variable in java programming. So lets go in the deep model.

What is variable

A variable is a storage area where we store the data and the information. A variable is a holder which holds the value while the java program is executed. Variable also declare with a data type. When we declare a variable name the first letter of the variable should not be a digit. And the variable name should not be a keyword.

String name;
int number;

Here string is a data type and name is a variable of this data type.

Types of variable:-

1. Local Variable:-

A local variable always declares inside the body of the method and function. A local variable can be a static variable. Local variable should be initialized before accessing them.

Class Test
{
public void sum()
{
int a, b=30, c=20; // declaring local variable a, b, c
a = b + c;
System.out.println(“Sum=”+a);
}
public static void main(String[] args)
{
Test obj= new Test(); // creating object
Obj.sum(); // calling object
}
}

Output

Sum = 50

2. Instance Variable:–

A variable that is declared inside a class but outside the body, method, constructor or block, is called instance variable. Instance variable need not be initialized before using them as they are automatically initialized to their default values.

class Test
{
int a, b=20, c=10;
public void add()
{
a = b + c;
System.out.println(“Sum=” +a );
}
public void sub()
{
a= b-c;
System.out.println(“Sub=”+a);
}
public static void main(String[] args)
{
Test obj= new Test();
obj.add( );
obj.sub( );
}
}

Output:-

Sum =30
Sum=10

3. Static Variable:-

A variable which is declared with the static keyword, Static variable is stored in the static memory. A static variable is regular to every one of the occasions (or objects) of the class since it is a class-level variable. you can say that only a single copy of the static variable is created and shared among all the instances of the class. Memory designation for such factors possibly happens once when the class is loaded in the memory.

Static variable Syntax

static keyword followed by data type and variable name

static data_type variable_name;

Static variable example in Java

class VariableDemo
{
static int count=0;
public void increment()
{
count++;
}
public static void main(String args[])
{
VariableDemo obj1=new VariableDemo();
VariableDemo obj2=new VariableDemo();
obj1.increment();
obj2.increment();
ystem.out.println(“Obj1: count is=”+obj1.count);
System.out.println(“Obj2: count is=”+obj2.count);
}
}

Output

Obj1: count is=2
Obj2: count is=2

As should be obvious in the above model that both the items are having a similar duplicate of static variable that is the reason, they shown a similar estimation of check.

variables in java

--

--

--

Use My Notes is an Education Website for providing all Technical course material. https://usemynotes.com/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Flutter: How to pass data to and back from a dialog box

Sum of Flutter and Dialog boxes.

Enterprise Architecture: Engineering Platforms

FIX Protocol: How electronic trading communicates?

Make “Headless” Raspberry Pi VNC Open in 1080p

Why Xamarin is Bad?

How did I manage to get my first Full-Stack Developer job without a degree.

Golang CRUD MySQL

Python 3 Migration- Valuable Lessons

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Use My Notes

Use My Notes

Use My Notes is an Education Website for providing all Technical course material. https://usemynotes.com/

More from Medium

Java Date Time API’s

Pattern matching for switch in java 17.

Easy way to set your Java Home

Assertions In Java