Constructors
Learn how constructors initialize objects when they are created.
Java Code
Study the example and understand what each part does.
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Main {
public static void main(String[] args) {
Student student =
new Student("Brian", 18);
System.out.println(student.name);
System.out.println(student.age);
}
}
Java code normally runs using the Java Development Kit on a computer, Android Studio or another Java development environment. This course page presents the code examples without pretending that the browser is running Java directly.
Expected Output / Result
Brian 18
Finished this lesson?
Mark it complete to update your course progress.