public class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("John", 20);
Student s2 = new Student("Jane", 22);
s1.display();
s2.display();
}
}
In this example, we have defined a class called "Student
" that has two instance variables - "name
" and "age
" - and a constructor that takes two parameters to initialize these variables.
In the main method, we have created two instances of the Student class - s1
and s2
- by calling the constructor and passing in the name and age values. We have then called the "display
" method on each object to print out their values.
Name: John
Age: 20
Name: Jane
Age: 22