java / beginner
Snippet
Creating Objects with 'new'
In Java, we use the 'new' keyword to create an instance of a class, which is called an object.
snippet.java
1
2
3
4
5
6
class Car {String color;}Car myCar = new Car();myCar.color = "Red";
Breakdown
1
class Car {
Defines a simple blueprint for a Car.
2
Car myCar = new Car();
Creates a new object instance of the Car class.
3
myCar.color = "Red";
Sets the color property of the specific object instance.