Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using local class in practice [duplicate]

When should we use local classes? The Best practice approach?

Is it possible to make them abstract or static?

Any other solution to avoid them?

like image 969
Med Besbes Avatar asked Nov 17 '25 08:11

Med Besbes


2 Answers

From document :

As mentioned in the section Nested Classes, nested classes enable you to logically group classes that are only used in one place, increase the use of encapsulation, and create more readable and maintainable code. Local classes, anonymous classes, and lambda expressions also impart these advantages; however, they are intended to be used for more specific situations:

Local class: Use it if you need to create more than one instance of a class, access its constructor, or introduce a new, named type (because, for example, you need to invoke additional methods later).

Anonymous class: Use it if you need to declare fields or additional methods.

Lambda expression:

  • Use it if you are encapsulating a single unit of behavior that you want to pass to other code. For example, you would use a lambda
    expression if you want a certain action performed on each element of
    a collection, when a process is completed, or when a process
    encounters an error.
  • Use it if you need a simple instance of a functional interface and none of the preceding criteria apply (for example, you do not need a
    constructor, a named type, fields, or additional methods).

Nested class: Use it if your requirements

  • are similar to those of a local class, you want to make the type more widely available, and you don't require access to local variables or method parameters.

  • Use a non-static nested class (or inner class) if you require access to an enclosing instance's non-public fields and methods. Use a static nested class if you don't require this access.

You can define a local class in a method body, a for loop, or an if clause. They are similar to Inner class, because they cannot define or declare any static members.

Also they are non-static because they have access to instance members of the enclosing block.

Also read this document for purpose of Local Classes.

like image 178
Saeed Masoumi Avatar answered Nov 19 '25 23:11

Saeed Masoumi


A local class is much like a local variable. You would create a local class if that class is only useful within a single method.

They can be declared abstract, but not static. As you might expect, a local class in a static method cannot access non-static instance fields or methods of the enclosing class.

There is no reason to avoid them, if they correctly and cleanly help you accomplish a task.

One use I've found is the construction of a Swing menubar hierarchy:

private JMenuBar buildMenuBar() {

    class MenuAction extends AbstractAction {
        private static final long serialVersionUID = 1;

        @Override
        public void actionPerformed(ActionEvent event) {
            // Deliberately empty.
        }
    }

    Action fileMenuAction = new MenuAction();
    Action editMenuAction = new MenuAction();
    Action viewMenuAction = new MenuAction();
    Action helpMenuAction = new MenuAction();

    localizer.configure(fileMenuAction, "menu.file");
    localizer.configure(editMenuAction, "menu.edit");
    localizer.configure(viewMenuAction, "menu.view");
    localizer.configure(helpMenuAction, "menu.help");

    JMenu fileMenu = new JMenu(fileMenuAction);
    JMenu editMenu = new JMenu(editMenuAction);
    JMenu viewMenu = new JMenu(viewMenuAction);
    JMenu helpMenu = new JMenu(helpMenuAction);

    // etc.
}

No other method will need the MenuAction class, so I made it local to the buildMenuBar method.

like image 36
VGR Avatar answered Nov 19 '25 23:11

VGR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!