Created
January 27, 2024 22:57
-
-
Save m0smith/11d4d483a7b03d47b5e123cace29f21f to your computer and use it in GitHub Desktop.
Example 2 for Default Constructor
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 Book { | |
String title; | |
// A no-argument constructor | |
Book() { | |
title = "Unknown"; | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
Book mysteryBook = new Book(); | |
System.out.println("Book title: " + mysteryBook.title); // Outputs: Book title: Unknown | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment