Created
January 27, 2024 22:59
-
-
Save m0smith/fc1cec3d69439e545e53c7812a498154 to your computer and use it in GitHub Desktop.
Default Constructor example 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Student { | |
String name; | |
int age; | |
// A parameterized constructor | |
Student(String newName, int newAge) { | |
name = newName; | |
age = newAge; | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
Student student1 = new Student("Alice", 20); | |
System.out.println("Student Name: " + student1.name + ", Age: " + student1.age); // Outputs: Student Name: Alice, Age: 20 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment