java / beginner
Snippet
Basic Class and Object Creation
In Object-Oriented Programming (OOP), a class is a blueprint. An object is an instance of that blueprint created using the 'new' keyword.
snippet.java
1
2
3
4
5
6
7
8
9
class Dog {String breed = "Labrador";void bark() {System.out.println("Woof!");}}Dog myDog = new Dog();myDog.bark();
Breakdown
1
class Dog
Defines a new type called 'Dog' with properties and behaviors.
2
Dog myDog = new Dog();
Instantiates the Dog class to create an object named 'myDog'.