Course Content
Core Java
About Lesson

Instance Variables

Data defined by the class are member or instance variables. Code that process/operate on that data is member methods. The methods and variables defined within a class are called members of class.

Variables defined within a class are called instance variables because each instance (object) of that class contains own copy of the variables.

Class contain method public static void main (String[] args) only if that is the starting point of the program.

How the implementation complexity or inner workings of class hidden:

  1. Each method or variable in a class can be marked as private or public.
    1. Public interface of class means those details are open for external users of a class.
  2. Private methods & data can only be accessed by code that is member of the class. External code (code outside the class) access private methods/data through the public methods (which can access the class’ private method/data).

Accessing Instance Fields and Methods

Fields and methods that are associated with an instance (object) of a class are accessed using the dot operator . on that object.

public class Vehicle {
    // Fields
    private String make;
    private String model;
    private int year;

    // Static field
    public static int vehicleCount = 0;

    // Constructor
    public Vehicle(String make, String model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
        vehicleCount++;
    }

    // Getter and Setter for make
    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    // Getter and Setter for model
    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    // Getter and Setter for year
    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    // Static method
    public static int getVehicleCount() {
        return vehicleCount;
    }

    // Instance method
    public void displayInfo() {
        System.out.println("Make: " + make);
        System.out.println("Model: " + model);
        System.out.println("Year: " + year);
    }
}

public class Main {
    public static void main(String[] args) {
        // Create instances of Vehicle class
        Vehicle vehicle1 = new Vehicle("Toyota", "Camry", 2020);
        Vehicle vehicle2 = new Vehicle("Honda", "Civic", 2019);

        // Access and modify instance fields via methods
        vehicle1.setMake("Ford");
        System.out.println("Vehicle 1 Make: " + vehicle1.getMake());

        // Display vehicle information
        vehicle1.displayInfo();
        vehicle2.displayInfo();

        // Access static field and method
        System.out.println("Total number of vehicles: " + Vehicle.getVehicleCount());
    }
}

Accessing Static fields and Methods

Static fields and methods belong to the class itself rather than any particular instance. They are accessed using the class name followed by the dot operator.

public class Vehicle {
    // Instance fields
    String make;
    String model;
    int year;

    // Static field
    static int vehicleCount = 0;

    // Constructor
    public Vehicle(String make, String model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
        vehicleCount++;
    }

    // Instance method
    public void displayInfo() {
        System.out.println("Make: " + make);
        System.out.println("Model: " + model);
        System.out.println("Year: " + year);
    }

    // Static method
    int getVehicleCount() {
        return vehicleCount;
    }
}

public class Main {
    public static void main(String[] args) {
        // Create instances of Vehicle class
        Vehicle vehicle1 = new Vehicle("Toyota", "Camry", 2020);
        Vehicle vehicle2 = new Vehicle("Honda", "Civic", 2019);

        // Access and modify instance fields via dot operator
        vehicle1.make = "Ford";
        System.out.println("Vehicle 1 Make: " + vehicle1.make);

        // Call instance method via dot operator
        vehicle1.displayInfo();
        vehicle2.displayInfo();

        // Access static field and method via dot operator
        System.out.println("Total number of vehicles: " + Vehicle.getVehicleCount());
    }
}

Scroll to Top