Java Tutorial : How To Convert Anonymous to Inner Refactoring On NetBeans

in #utopian-io6 years ago (edited)

Repository

https://github.com/dmlloyd/openjdk

What Will I Learn?

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

  • You will learn about refactoring on NetBeans
  • You will learn How Convert Anonymous to Inner Refactoring On NetBeans
  • You will learn about Anonymous and inner refactoring

Requirements

  • you must know basic of java programming
  • you must understand basic operation on Net Beans

Difficulty

Choose one of the following options:

  • Basic

Tutorial Contents

okay, on this occasion I will share a tutorial how to convert anonymous to inner refactoring.
Convert Anonymous to Inner refactoring is used to separate an anonymous inner class into an actual inner class. There are several varieties of anonymous inner classes:

  • Inner class for defining and instantiating an instance of an unnamed subclass.
  • Inner class for defining and instantiating an anonymous implementation of an interface.
  • Anonymous inner class defined as an argument to a method.

For this section, we focus on the first type: unnamed subclasses.
Suppose you have the following code:

  • open NetBeans and make new class for test.
  • and type this code:
public class Item {
    public void assemble() {
        System.out.println("Item.assemble");
        }
    }
  • the above program section is a program for experiments with item name.
  • and below is part of the experiment program with factory name.
public class Factory {
    public void makeStandardItem(int type) {
        if(type==0) {
        Item myitem = new Item() {
        public void assemble() {
        System.out.println("anonymous Item.assemble");
    }
};
myitem.assemble();
} else {
    Item myitem = new Item();
myitem.assemble();
    }
}
}
  • Okay I will explain a little about the script above.
  • The code declares a class Item with a method named assemble().
  • The Factory class defines a variable myitem of type Item and instantiates an anonymous subclass of Item that overrides the assemble() method.

Why would you bother using an anonymous inner class instead of a normal inner or outer class? In this example, if the one-off case where the anonymous inner class is used were the only area where it is needed, you might not want to create a separate class. However, if you find that you need the code in the anonymous subclass in multiple areas, you might want to convert it to an inner class.

  • To convert the code to an inner class, click anywhere inside the anonymous class or highlight the name of the Item class constructor in the following section of the code:
Item myitem = new Item() {
    public void assemble() {
        System.out.println("anonymous Item.assemble");
    }
};
  • then select refactor:

  • choose convert:

  • In the Convert Anonymous Class to Inner dialog box, you’ll see the default class name of NewClass , as shown in Figure 10-11.
  • You can set the name of the new inner class that will be created, the access level, and whether it should be declared static. If the constructor for the anonymous class has any parameters, the dialog box will also list them.

  • Suppose you named the new inner class StrangeItem. The refactored code would look like this:
private class StrangeItem extends Item {
    public void assemble() {
        System.out.println("anonymous Item.assemble");
    }
}
  • This class would be declared inside the Factory class, since that is where the original anonymous inner class resides.
  • In the following code, notice that the creation of the anonymous inner class has been
    altered to create an instance of the new inner class:
public void makeStandardItem(int type) {
    if(type==0) {
    Item myitem = new StrangeItem();
myitem.assemble();
} else {
    Item myitem = new Item();
    myitem.assemble();
    }
}
  • The purpose of this refactoring is to make your code more reusable and modular. Extracting the anonymous inner class into its own inner class helps improve many aspects of your code.
  • makes no sense to redefine the same anonymous inner class in multiple places in the Factory class, and Convert Anonymous to Inner refactoring can help correct the situation.

  • oke tutorial is fix, you can try it at home.

Curriculum

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

Proof of Work Done

Sort:  

Your post has been found to have copy pasted content from other sources. This is plagiarism, and the source is found here
A prior tutorial of yours is also plagiarized from this same source.
Plagiarism is a serious offense, and accordingly your account has been banned for 60 days from receiving Utopian reviews.
Future plagiarism will result in permanent ban from Utopian reviews.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

sorry sir @mcfarhat that is the material given by my lecturer at the university, so I don't know it's plagiarism

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