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.
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.
When the program is run, it prompts the user to input First Name.
Program prompts user to input Last Name
Program displays results.
Program displays a message if user exits the program prematurely.
GitHub Account
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
Hey @will-ugo I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
You are really doing a good job, keep steeming👏
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks @nmalove
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Changed
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit