Java Variables: A Comprehensive Guide

Variables

“A variable is an identifier whose value will be changed during executing of the program”.

Rules for writing variables:
1. First letter must be an alphabet.
2. The length of the variable should not exceed more than 32 characters.
3. No special symbols are allowed except underscore.
4. No keywords should use as variable names.

Types of variables in JAVA:

TYPES OF VARIABLES
  •  Whenever we develop any JAVA program that will be developed with respect to class
    only.
  • In a class we can use ‘n’ number of data members and ‘n’ number of methods.
  •  Generally in JAVA, we can use two types of data members or variables.They are Instance/non-static variables and static variables.
HTML CSS Tables
Sno Static Variable Instance Variable
1 Static variables are whose memory space is creating only once when the class is loaded by class loader sub system (JVM)in the main memory irrespective of number of objects. An instance variable is one whose memory space is creating each and every time whenever an object is created
2 Programmatically static variable declaration must be preceded by keyword static. Programmatically instance variable declaration should not be preceded by keyword static.
3 Static variables must be accessed with respect to class name i.e., classname.varname; Instance variable must be accessed with respect to object name i.e., objectname.varname;
4 Value of static variable is always recommended for sharable. Value of instance variable is not sharable.
5 Static variable are also known as class level data members since they are dependent on classes. Instance variable are also know as object level data members since they are dependent on objects.

Static Variables Example

public class Counter {
// Static variable
static int count = 0;

// Instance method to increment the static variable
public void increment() {
count++;
}

public static void main(String[] args) {
// Creating objects of the Counter class
Counter counter1 = new Counter();
Counter counter2 = new Counter();

// Calling instance methods to increment the static variable
counter1.increment();
counter2.increment();

// Accessing the static variable
System.out.println(“Count: ” + Counter.count); // Output: 2
}
}

Instance Variables Example

public class Car {
// Instance variables
String brand;
String model;
int year;

// Constructor
public Car(String brand, String model, int year) {
this.brand = brand;
this.model = model;
this.year = year;
}

// Instance method
public void displayDetails() {
System.out.println(“Brand: ” + brand);
System.out.println(“Model: ” + model);
System.out.println(“Year: ” + year);
}

public static void main(String[] args) {
// Creating objects of the Car class
Car car1 = new Car(“Toyota”, “Camry”, 2022);
Car car2 = new Car(“Honda”, “Accord”, 2021);

// Accessing and modifying instance variables
car1.year = 2023;

// Calling instance method to display details
car1.displayDetails();
car2.displayDetails();
}
}

Leave a Comment

Your email address will not be published. Required fields are marked *