Questions regarding `class Demo`

I have noticed that in this last subjects, you have used the Demo class to execute the program with prints. Why don’t we just print in the constructor or in the main class? Sorry if my question is too dumb.


Hi @Emilio_Parra !!
Your question is not dumb at all! It’s great that you’re seeking clarification.

class Date {

  private int day;
  private int month;
  private int year;

  // Default constructor
  public Date() {
    // We must define the default values for day, month, and year
    day = 0;
    month = 0;
    year = 0;

  // Parameterized constructor
  public Date(int d, int m, int y){
    // The arguments are used as values
    day = d;
    month = m;
    year = y;

  // A simple print function
  public void printDate(){ 
    System.out.println("Date: " + day + "/" + month + "/" + year);

class Demo {
  public static void main(String args[]) {
    // Call the Date constructor to create its object;
    Date date = new Date(1, 8, 2018); // Object created with specified values! // Object created with default values!

In the code , the purpose of the Demo class is to demonstrate the usage of the Date class. It serves as a separate class that contains the main method, which is the entry point of the Java program. The main method is automatically executed when you run the program.

By placing the code that demonstrates the usage of the Date class in the main method of the Demo class, it allows for a clear separation of concerns. The Date class is responsible for managing date-related operations, while the Demo class is responsible for showcasing how the Date class can be used.

This separation of concerns is a common practice in software development. It helps to improve code organization, readability, and maintainability. Additionally, it makes it easier to understand and test individual components of the program.

Printing the output directly within the constructors or the main method is possible, but it may not be ideal in all situations. For example, if you have multiple constructors or if you want to demonstrate different use cases of the Date class, having a separate class like Demo allows you to organize and present the code in a more structured manner.

In summary, using the Demo class to execute the program with prints helps in separating concerns, improving code organization, and presenting the code in a more structured way for demonstration purposes.
I hope it helps. Happy Learning


Now it is clear! Thank you so much