Java Classes and Objects
A class is a blueprint; an object is one thing built from it. Classes bundle fields (data) and methods (behaviour).
ExampleJava
public class Main {
static class Dog {
String name;
int age;
void bark() {
System.out.println(name + " says woof");
}
}
public static void main(String[] args) {
Dog rex = new Dog();
rex.name = "Rex";
rex.age = 3;
rex.bark();
Dog fido = new Dog();
fido.name = "Fido";
fido.bark();
}
}Output
Rex says woof Fido says woof
Quick check
Which keyword creates an object?
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.