Mac Java Installation Steps#
- Download and Install Java
- Check installed JDKs
/usr/libexec/java_home -V [for Mac]java -version [for Windows]
- 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]
- Save and reload shell config in terminal
- Check Java HOME is setup properly from terminal
Windows Java Installation Steps#
- Download and Install Java
- Check installed JDKs
java -version [for Windows]
- 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
- Update PATH Variable
- In the same Environment Variables window, select Path → Edit → New, then add:
- Click OK to save and close
- Verify Configuration in Command Prompt
java -versionecho %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.