Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java inner class usage and instantiation

Some time ago I wrote a little image viewer/processing program with Java, a mini-Photoshop, if you will.

I wanted there to be a drop-down menu where I could select which one of the images I have opened would be "on the table", ie. shown and methods applied to. I wanted the name of the image to be the name of the JMenuItem shown in the menu. I also wanted a new button to appear when I add a new image.

I wondered this for some time and finally produced this solution, a new class that handles the creation of the new button when an image is added. The code is as follows:

import java.awt.event.*;
import javax.swing.*;
import java.util.*;


public class ImageList{

    private ArrayList<JMenuItem> list;
    private ImageHandler main;
    private ImageLevel img;

    public ImageList() {}

    public void setHandler(ImageHandler hand) {
        main = hand;
        img = main.getImg1();
    }

    public void add(Buffer addi) {
        final String added = addi.getName();
        JMenuItem uusi = new JMenuItem(added);

        main.getMenu5().add(uusi);
        uusi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                img.setBuffer(added);
                main.getScr().updateUI();
            }
        });
   }
}

This works as it should. For this site I translated the original Finnish names to English, wonder why I wrote them in Finnish originally...I suck at naming things.

Method add is supposed to be called multiple times while the program is running.

What I cannot understand really is the inner class implementation of the interface ActionListener, namely its compilation and how it works.

If I have two buttons on my interface and I want them to do different things, I need two inner classes, one for each, each having its own inner implementation of the interface ActionListener. But in my code there is one class that seems to do the work of many, one complied .class-file for it, but the final result works as if there were many.

Can someone educate me on this issue? Is this code here one class and new buttons are instances of it? Are they new classes? Should there be a new .class-file for each new button? etc...

like image 348
Valtteri Avatar asked Nov 16 '12 16:11

Valtteri


People also ask

Can you instantiate in inner class Java?

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass outerObject = new OuterClass(); OuterClass. InnerClass innerObject = outerObject.

What is the use of inner class in Java?

Java inner class or nested class is a class that is declared inside the class or interface. We use inner classes to logically group classes and interfaces in one place to be more readable and maintainable. Additionally, it can access all the members of the outer class, including private data members and methods.

Can static inner class be instantiated?

A static inner class can be instantiated without the need for an instance of the outer class. In general, an Inner class is a part of nested class, called Non-static nested classes in Java. The types of inner classes are member inner class, anonymous inner class, and local inner class.

Why would you use an inner class?

Inner classes are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a member of other class, then the inner class can be made private. And this is also used to access the private members of a class.


1 Answers

Often, an inner class is instantiated in a code which is only called once (e.g. when you extend JPanel and add ActionListeners to JButtons in the constructor). Here, you instantiate an inner class in a method which you call several times (if I understand your description correctly). Each time you call add(), a new instance of the inner class will be created. As with named classes, there is only one class, but there may be many instances.

like image 168
Code-Apprentice Avatar answered Oct 07 '22 03:10

Code-Apprentice