Java Program with Constructor

Java Program with Constructor
Reading Time: 6 minutes

Introduction:

The provided Java program defines a Person class with fields for a person’s name and age. It includes a constructor to initialize these fields and a method to display the person’s information. In the main method, an instance of the Person class is created, and the information is displayed.

Algorithm:

  1. Class Definition:
    • Define a class named Person with private fields: name (String) and age (int).
  2. Constructor:
    • Create a constructor that takes parameters for name and age and initializes the corresponding fields.
  3. Method to Display Information:
    • Implement a method named displayInfo to print the person’s name and age.
  4. Main Method:
    • Inside the main method:
      • Create an instance of the Person class using the constructor, providing values for name and age.
    • Display the person’s information using the displayInfo

Explanation:

The Person class encapsulates the data related to an individual, and its constructor ensures that a new person object is properly initialized with a name and age. The displayInfo method provides a way to output the person’s details in a structured format.

In the main method, an instance of the Person class, named person1, is created with the name “John Doe” and age 25. Subsequently, the displayInfo method is called on this instance, printing the person’s name and age to the console.

Code :

				
					public class Person {
    // Fields
    private String name;
    private int age;
    // Constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    // Method to display information
    public void displayInfo() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
    public static void main(String[] args) {
        // Creating an instance of Person using the constructor
        Person person1 = new Person("John Doe", 25);

        // Displaying information using the displayInfo method
        person1.displayInfo();
    }
}

				
			

Output :

				
					Name: John Doe
Age: 25
				
			

In this program, there’s a Person class with private fields name and age, a constructor that initializes these fields, and a displayInfo method to print the person’s information. The main method creates an instance of the Person class using the constructor and then displays the information using the displayInfo method.

Overall, this program demonstrates the principles of encapsulation, constructor usage, and method invocation in Java, providing a basic structure for working with person-related data.Top of Form

FAQ’s :

  1. How do I create a Java program with a constructor?

Answer: To create a Java program with a constructor, define a class and include a constructor method within it. The constructor initializes the class’s fields when an object is instantiated.

  1. Can you provide an example of a Java program constructor?

Answer: Certainly! Here’s a simple Java program with a constructor. The constructor initializes the name and age fields of a Person class.

  1. What is the significance of a Java program on constructor?

Answer: In a Java program, constructors play a crucial role in initializing object state. They ensure that objects are properly set up when created, contributing to the overall integrity and functionality of the program.

  1. How can I write a Java program using constructor for object initialization?

Answer: To write a Java program using a constructor for object initialization, define a class with a constructor method. When creating an instance of the class, the constructor is automatically invoked to initialize the object’s attributes.

  1. What is Java program constructor overloading and why is it useful?

Answer: Java program constructor overloading involves defining multiple constructors within a class, each with a different parameter list. This allows flexibility in creating objects with varying initializations, catering to different use cases and enhancing the versatility of the program.