DEV Community

Subash
Subash

Posted on

Variables and Objects in Java

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:

  1. Local Variables
  2. Instance Variables (Non-static)
  3. 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.println(age);
}
Enter fullscreen mode Exit fullscreen mode

Here, age is a local variable.

2. Instance Variables (Non-static)

Declared inside a class but outside methods and constructors.

  • Each object gets its own copy.
  • Can have different values for different objects.
  • Receive default values if not initialized.
class Student {
    String name;
    int age;
}
Enter fullscreen mode Exit fullscreen mode

Here, name and age are instance variables.

3. Static Variables

Declared inside a class using the static keyword.

  • Only one shared copy exists for the class.
  • All objects share the same variable.
  • Can be accessed using the class name.
class Student {
    static String college = "ABC College";
}
Enter fullscreen mode Exit fullscreen mode

Here, college is a static variable shared by all Student objects.

Remember: Instance variables belong to objects, whereas static variables belong to the class.


What is an Object?

An object is an instance of a class.

A class is a blueprint used to create objects.

Every object has three characteristics:

  • State: Properties or data (model, price).
  • Behavior: Actions (drive, brake).
  • Identity: Distinguishes one object from another.

Creating a Class and Object

Let's understand this with a simple Car example.

Example:

public class Cars {

    String model;  // Instance variable
    int price;     // Instance variable

    void drive() {  // Instance method
        System.out.println("Car is driving...");
    }

    public static void main(String[] args) {

        Cars myCar = new Cars();  // Object creation

        myCar.model = "XUV";
        myCar.price = 600000;

        myCar.drive();  // Method calling
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Car is driving...
Enter fullscreen mode Exit fullscreen mode

Understanding the code:

  • Cars – Class name.
  • model and price – Instance variables.
  • drive() – Instance method (non-static).
  • new Cars() – Creates an object.
  • myCar – Reference variable that refers to the object.
  • myCar.drive() – Calls the object's method.

Top comments (0)