Java Constructors Using Eclipse IDE

in utopian-io •  7 years ago  (edited)

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

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hey @will-ugo I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

You are really doing a good job, keep steeming👏

Thanks @nmalove

Your contribution cannot be approved yet because it is attached to the wrong repository. Please edit your contribution and fix the repository to reapply for approval.

You may edit your post here, as shown below:

You can contact us on Discord.
[utopian-moderator]

Changed

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by will-ugo from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews/crimsonclad, and netuoso. The goal is to help Steemit grow by supporting Minnows and creating a social network. Please find us in the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP. Be sure to leave at least 50SP undelegated on your account.

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]