Java Tutorial : How To Encapsulate Fields Refactoring On NetBeanssteemCreated with Sketch.

in utopian-io •  6 months ago


What Will I Learn?

In brief, write details of what the user is going to learn in a bullet list.

  • You will learn how to encapsulate field rafactoring on java
  • You will learn how to encription java file
  • You will learn how to about refactoring


State the requirements the user needs in order to follow this tutorial.

  • you must know basic of java
  • you must understand how to aplicated NetBeans


Choose one of the following options:

  • Basic

Tutorial Contents

okay on this occasion I will share a tutorial on how to encapsulate a refactoring fileds on Netbeans, this encapsulation works so that the data we input becomes consistent and accurate.

  • okay, you have to make a new java file for the experiment.
  • This is an example of a problem I tried.
  • When writing applications, it is often useful to represent objects in the real world as classes with attributes. For example, you may choose to represent the fields for an employee as an Employee class with first name and last name public members:
    public class Employee { public String FirstName; public String LastName; }
  • Of course, you might also include address, phone number, organizational, and personal fields
    in the class.
  • Such an Employee class is quick and easy to work with, such as in the following code:
public class NewHire {
public static void main(String[] args) {
Employee newemp = new Employee();
newemp.FirstName = args[0];
newemp.LastName = args[1];
  • In the NewHire class, an instance of Employee is instantiated and the FirstName and LastName fields are set from the arguments passed on the command line. (Obviously, there are a lot of problems with the code in the NewHire class, such as no parameter or error checking, but here we are just focusing on the topic of encapsulation.)
  • Encapsulation involves controlling access to a class member variable using getter and setter methods. The class member variable is set to private so that no code outside the class can interact with it. The getter and setter methods are usually given a public accessor so that any code can retrieve or set the value of the member variable.
  • In the following code, the Employee class has been modified to use getters and setters for the FirstName and LastName member variables:
public class Employee {
private String FirstName;
private String LastName;
public void setFirstName(String FirstName) {
this.FirstName = FirstName;
public String getFirstName() {
return this.FirstName;
public void setLastName(String LastName) {
this.LastName = LastName;
public String getLastName() {
return this.LastName;
  • You can also modify the code in the NewHire class to interact with the updated Employee class. The NewHire class must now use the getter and setter methods:
public class Employee {
private String FirstName;
private String LastName;
public void setFirstName(String FirstName) {
if(FirstName!=null) {
this.FirstName = FirstName.toUpperCase();
} else {
this.FirstName = null;
public String getFirstName() {
return this.FirstName;
public void setLastName(String LastName) {
if(LastName!=null) {
this.LastName = LastName.toUpperCase();
} else {
this.LastName = null;
public String getLastName() {
return this.LastName;
  • this is the step of determining the field you want to encapsulate.

  • With this type of design, you are in a better position to modify the code to handle special conditions. In the example, the code in the Employee class can be modified to convert the member variables to uppercase when they are set using Employee.setFirstName and Employee.setLastName:
    NOTE :
  • It is usually preferable to perform any data conversion, checking, or modification in the setter method
  • for a member variable rather than in the getter method. If the data conversion is implemented in the getter, each time the data is retrieved, the data conversion will take place, thus reducing performance slightly.

  • Okay, now we will see the encapsulate dialog box, whether the fields have been encapsulated.

    encapsulate fields refactoring dialog

  • okay, we have successfully encapsulated the refactoring fields that we want.
  • the tutorial ends, don't forget to trying it guys ...!


Include a list of related tutorials you have already shared on Utopian that make up a Course Curriculum, if applicable.

Proof of Work Done

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:  

Thank you for your contribution @lapulga.
After reviewing your contribution we suggest you make the following improvements in your tutorials:

  • We suggest you write comments in your code. It is important for users who are reading your tutorial to understand your code well.

  • Your tutorial is quite short for a good tutorial. We recommend you aim for capturing at least 2-3 concepts.

  • The tutorial is too basic and there are already some tutorials on how to encapsulate fields refactoring on netbeans. See this link

  • Please indent your code. Read this article, explains why the importance of the indented code.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.

Need help? Write a ticket on
Chat with us on Discord.


Thank you for your review, @portugalcoin!

So far this week you've reviewed 6 contributions. Keep up the good work!

Hey, @lapulga!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support!
Simply set as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord

Vote for Utopian Witness!

Hi @lapulga!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server