What is Java Class Example?
Here's a simple example of a Java class:
public class Dog {
// Attributes
String name;
int age;
// Constructor
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
// Method
public void bark() {
System.out.println(name + " says Woof!");
}
public static void main(String[] args) {
Dog myDog = new Dog("Buddy", 3);
myDog.bark(); // Output: Buddy says Woof!
}
}
This class defines a Dog
with attributes, a constructor, and a method to make it bark.