Learn GETTER and SETTERS in 10 minutes! 🔐

Learn GETTER and SETTERS in 10 minutes! 🔐

22.927 Lượt nghe
Learn GETTER and SETTERS in 10 minutes! 🔐
#java #javatutorial #javacourse public class Main { public static void main(String[] args) { // They help protect object data and add rules for accessing or modifying them. // GETTERS = Methods that make a field READABLE. // SETTERS = Methods that make a field WRITEABLE. Car car = new Car("Charger", "Yellow", 10000); car.setColor("Blue"); car.setPrice(5000); System.out.println(car.getColor() + " " + car.getModel() + " " + car.getPrice()); } } public class Car { private String model; private String color; private int price; Car(String model, String color, int price){ this.model = model; this.color = color; this.price = price; } String getModel(){ return this.model; } String getColor(){ return this.color; } String getPrice(){ return "$" + this.price; } void setColor(String color){ this.color = color; } void setPrice(int price){ this.price = price; } }