What is Create A Class In Java And Create Two Instances?
To create a class in Java, define its attributes and methods. Here’s a simple example:
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Person person1 = new Person("Alice", 30);
Person person2 = new Person("Bob", 25);
person1.display();
person2.display();
}
}
This code creates a Person
class and two instances: person1
and person2
.