Java Varibles and Identifiers

 

 Variables in Java

Definition:

A variable is a named memory location used to store data. Its value can change during program execution.

Types of Variables in Java:

(1) Local Variables:-

  • Declared inside methods/blocks.
  • Must be initialized before use.
Syntax:
       datatype variableName;

Example:










(2) Instance Variables:-
  • Declared inside a class but outside methods.
  • Each object has its own copy.

Syntax:

class ClassName {
datatype variableName;
}
Example:

(3) Static Variables:-
  • Declared using static keyword.
  • Shared among all objects.
Syntax:

static datatype variableName;
Example:

























Identifiers in Java

Definition:

Identifiers are the names given to variables, methods, classes, and objects.

Rules for Identifiers:

  • Must begin with a letter, underscore (_), or dollar ($)
  • Cannot start with a digit
  • Cannot be a keyword (e.g., int, class)
  • Can contain letters, digits, _, $
  • Java is case-sensitive

Syntax:-

datatype identifierName;

Example:-
























Comments

Popular posts from this blog

Java Introduction

Java Datatypes