Menu

Types of Variables in Java
📰

Types of Variables in Java

DEV Community: java·ABISHEK M·18 days ago
#wUsZ5guv
#devtojava#devto#dev#variable#static
Long article (841 words) — no AI summary yet.
Reading 0:00
15s threshold

1. Introduction

A variable is a named memory location used to store data in a Java program.

For example:

int age = 21 ; String name = "Abishek" ; double salary = 25000.50 ;

Enter fullscreen mode

Exit fullscreen mode

In this example:

age stores an integer value.

name stores a String value.

salary stores a decimal value.

Variables are important because they allow us to store, access, and modify data while a program is running.

2. Types of Variables in Java

Java has three main types of variables:

Local Variable

Instance Variable

Static Variable

A. Local Variable

A variable declared inside a method, constructor, or block is called a local variable.

Example:

class Student {

void display ( ) {

int age = 21 ;

System . out . println ( age ) ; }

public static void main ( String [ ] args ) {

Student s1 = new Student ( ) ;

s1 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.