Java Constructors Using Eclipse IDE

Words
358
Reading
2 min
Listen
Play
9y

Constructors unlike Methods is a block of code that lets you initialize variables immediately an object is created. Methods can be created and called later but with constructors, initialization is done immediately. Constructors have the same name as the class name and is called when an object of the class in created.

1.png

2.png

This program illustrates how constructors are created and initialized once an object is created. The program opts the user to input two names and then displays the names when the program is run.

Constructor Class

Line 1: Import Statement

Line 3: Package Name

Line 4: Variable Declaration

Line 5: Variable Declaration

Line 7: Constructor created with two String Arguments

Line 8: Assignment Statement

Line 9: Assignment Statement

Line 10: Constructor Closing Brace

Line 12: Method created to return a value

Line 13: Method return Statement

Line 14: Method Closing Brace

Line 16: Method created to return a value

Line 17: Method return Statement

Line 18: Method Closing Brace

Line 20: Method created to display a String.

Line 21: Message is displayed on the screen.

Main Class

Line 1: Import Statement

Line 3: Package Name

Line 4: Main Method

Line 5: Variable Declaration and Initialization

Line 7: if Statement with condition

Line 8: Message is displayed on the screen

Line 9: Program Ends if condition is true.

Line 10: if Statement Closing Brace

Line 11: else Statement

Line 12: Variable Declaration and Initialization

Line 13: if Statement and condition

Line 14: Message is displayed on the screen

Line 15: Program ends if condition is true

Line 16: if Statement Closing Brace

Line 17: else Statement

Line 18: An object of the constructor class is created and initialized with two String Variables immediately.

Line 19: Object created is used to call Method from the constructor class and it displays the output.

3.png

When the program is run, it prompts the user to input First Name.

4.png

Program prompts user to input Last Name

5.png

Program displays results.

6.png

Program displays a message if user exits the program prematurely.

7.png

GitHub Account

8.png

9.png

Source Codes from GitHub Account

CODE BLOCK

Constructor Class

import javax.swing.JOptionPane;
public class constructor {
    public String firstName;
    public String lastName;
    public constructor(String fName, String lName) {
        firstName = fName;
        lastName = lName;
    }
    public String getFirstName() {
        return firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void displayMessage() {
        JOptionPane.showMessageDialog(null, "Your Name is " + getFirstName() + " " + getLastName());
    }

}

Main Class

import javax.swing.JOptionPane;
public class mainClass {
    public static void main (String args []) {
        String firstNameString = JOptionPane.showInputDialog("Enter Your First Name");
        if (firstNameString == null) {
            JOptionPane.showMessageDialog(null, "Program Ended");
            System.exit(0);
        }
        else {
            String lastNameString = JOptionPane.showInputDialog("Enter Your Last Name");
            if (lastNameString == null) {
                JOptionPane.showMessageDialog(null, "Program Ended");
                System.exit(0);
            }
            else {
            constructor userName = new constructor(firstNameString, lastNameString);            
            userName.displayMessage();
            }
        }
    }
}

Thanks for reading.



Posted on Utopian.io - Rewarding Open Source Contributors

Java Constructors Using Eclipse IDE | Ecency