Mac Java Installation Steps

  1. Download and Install Java
  2. Check installed JDKs
  • /usr/libexec/java_home -V [for Mac]
  • java -version [for Windows]
  1. Open .zshrc and set JAVA_HOME
  • vi ~/.zshrc [using terminal]
  • export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home [add this]
  • esc → :wq [Save and exit]
  1. Save and reload shell config in terminal
  • source ~/.zshrc
  1. Check Java HOME is setup properly from terminal
  • echo $JAVA_HOME

Windows Java Installation Steps

  1. Download and Install Java
  2. Check installed JDKs
  • java -version [for Windows]
  1. Set JAVA_HOME Environment Variable
  • Open System Properties → Advanced → Environment Variables
  • Under System variables, click New and add:
    • Variable name: JAVA_HOME
    • Variable value: C:\Program Files\Java\jdk-17 [or your JDK path]
  • Click OK to save
  1. Update PATH Variable
  • In the same Environment Variables window, select Path → Edit → New, then add:
    • %JAVA_HOME%\bin
  • Click OK to save and close
  1. Verify Configuration in Command Prompt
  • java -version
  • echo %JAVA_HOME%

Class-level/Instance variables/field + Local Variable

public class Car {

    String brand; //  Class-level/Instance variables/field (each Car object has its own copy)
    int year;
    static int wheels = 4; //Static variable (shared by ALL Car objects)
    
    public Car(String brand, int year) {       // Constructor (sets instance variables)
        this.brand = brand;
        this.year = year;
    }
    
    public void displayInfo() {   // Method with a local variable
        String info = "Car Brand: " + brand + ", Year: " + year + ", Wheels: " + wheels;  //  Local variable (exists only while this method runs)
        System.out.println(info);
    }

    public static void main(String[] args) {

        Car myCar = new Car("Toyota", 2022);        //  Local variable in main()
        myCar.displayInfo();   // Calling instance method using the object
        System.out.println("All cars have " + Car.wheels + " wheels."); // Accessing static variable

        Car anotherCar = new Car("Honda", 2020); // Creating another car to show that static variable is shared
        anotherCar.displayInfo();
    }
}

When to use static

  • You’d only make it static if the method doesn’t depend on any specific object’s data. For example: public static void showWheelsInfo() { System.out.println(“All cars have " + wheels + " wheels.”); } This one is static because it only uses the wheels variable — which is also static — and doesn’t need to know about a specific Car object.