Create an ARRAY OF OBJECTS in 5 minutes!

Video Player is loading.
Current Time 0:00
Duration -:-
Loaded: 0%
Stream Type LIVE
Remaining Time -:-
 
1x
8 Views
Published
#java #javatutorial #javacourse

public class Main {
public static void main(String[] args) {

Car[] cars = {new Car("Mustang", "Red"),
new Car("Corvette", "Blue"),
new Car("Charger", "Yellow")};

for(Car car : cars){
car.color = "black";
}

for(Car car : cars){
car.drive();
}
}
}

public class Car {

String model;
String color;

Car(String model, String color){
this.model = model;
this.color = color;
}

void drive(){
System.out.println("You drive the " + this.color + " " + this.model);
}
}
Category
Bro Code
Tags
java tutorial, java course, java programming
Show more
Be the first to comment