Skip to content

Instantly share code, notes, and snippets.

@m0smith
Created January 27, 2024 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m0smith/fc1cec3d69439e545e53c7812a498154 to your computer and use it in GitHub Desktop.
Save m0smith/fc1cec3d69439e545e53c7812a498154 to your computer and use it in GitHub Desktop.
Default Constructor example 3
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