INSTANCE_VARIABLES:
In Java, an instance variable is a variable that is declared inside a class but outside of any method, constructor, or block. It is associated with a specific instance (object) of the class. Each object of the class has its own copy of the instance variable.
Key Points:
Declared inside a class but outside methods.
Each object has its own copy.
Initialized to default values if not explicitly initialized.
Can have any access modifier (private, public, etc.).
STRUCTURE:
class ClassName {
## Instance variables ## (non-static fields)
datatype variableName1;
datatype variableName2;
}
EXAMPLE;
public class Person {
Instance variables
String name;
int age;
}
Method to display instance variables
void showDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String[] args) {
Creating objects
Person p1 = new Person("Alice", 25);
Person p2 = new Person("Bob", 30);
Displaying values of instance variables
p1.showDetails();
System.out.println("-----");
p2.showDetails();
}
}
Output:
Name: Alice
Age: 25
Name: Bob
Age: 30
REFERENCE LINK :Java Variables - Oracle Docs
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
LOCAL_VARIABLES:
A local variable is a variable declared inside a method, constructor, or block, and it exists only while the method is executing. It is not accessible outside the method or block.
STRUCTURE:
class ClassName {
void methodName() {
# Local variable #
datatype variableName = value;
System.out.println(variableName);
}
EXAMPLE :
public class Example {
void displayMessage() {
// Local variable
String message = "Hello, this is a local variable!";
System.out.println(message);
}
public static void main(String[] args) {
Example ex = new Example();
ex.displayMessage();
}
}
Output:
Hello, this is a local variable!
Key Notes:
message is a local variable.
It is declared inside the displayMessage() method.
It cannot be used outside that method.
Official Reference Link:
Java Variables - Oracle Docs
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
STATIC_METHOD:
A static method is a method that belongs to the class rather than any object. It can be called without creating an object of the class.
Key Points:
Declared using the static keyword.
Can access only static variables and static methods directly.
Cannot use this keyword.
Commonly used for utility or helper functions.
Structure:
class ClassName {
// Static method
static void methodName() {
// logic
}
public static void main(String[] args) {
// Call static method without object
ClassName.methodName();
}
}
Example:
public class Utility {
static void greet() {
System.out.println("Hello from static method!");
}
public static void main(String[] args) {
Utility.greet(); // No object needed
}
}
OUTPUT;
Hello from static method!
Official Reference Link:
Oracle Docs – Understanding Class Methods (Static)
https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
STATIC_VARIABLES:
A static variable is a variable that is shared among all instances (objects) of a class. It is declared using the static keyword and belongs to the class, not to individual objects.
Key Points:
Declared with the static keyword.
Shared across all objects of the class.
Initialized only once, at class loading time.
Can be accessed using the class name or an object.
Stored in the method area of memory.
Syntax:
class ClassName {
static datatype variableName;
}
EXAMPLE:
public class Demo {
// Static variable
static int x = 10;
public static void main(String[] args) {
// Access static variable using class name
System.out.println("Value of x: " + Demo.x);
}
}
Output:
Value of x: 10
reference link – Static (Class) Variables
🔗 Link: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
Top comments (0)