Menu

Variables and Objects in Java
πŸ“°

Variables and Objects in Java

DEV Community: javaΒ·SubashΒ·16 days ago
#rQaaTRVM
#devtojava#devto#dev#variables#class
Reading 0:00
15s threshold

What is a Variable?

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

Syntax:

dataType variableName = value ;

Enter fullscreen mode

Exit fullscreen mode

Example:

int age = 25 ; String name = "John" ; double salary = 25000.50 ;

Enter fullscreen mode

Exit fullscreen mode

Rules for Creating Variables

Names can contain letters, digits, _ and $ .

A variable name cannot start with a digit.

Spaces are not allowed.

Java keywords cannot be used as variable names.

Variable names are case-sensitive ( age and Age are different).

Use meaningful names and follow camelCase.

Types of Variables in Java

Java has three types of variables:

Local Variables

Instance Variables (Non-static)

Static Variables

1. Local Variables

Declared inside a method, constructor, or block.

Accessible only within that method or block.

Must be initialized before use.

Do not receive default values.

void display () { int age = 25 ; System . out .…

Continue reading β€” create a free account

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