Search code examples
javasyntaxequivalent

unfamiliar Java syntax '(new MyClass.1())'


Some code I'm maintaining uses unfamiliar syntax. I haven't been able to find examples of this syntax in the Java docs.

public static void main(String[] args){ ... javax.swing.SwingUtilities.invokeLater(new MyClass.1()); ... }

and

public MyClass(a,m){ ... javax.swing.myJButton.addActionListener(new MyClass.5(this)); ... }

Q1. What do '.1' and '.5' mean and do?

Q2. What does '(this)' do? Is it shorthand for (this.param1, this.param2,...)?

Q3. Is this syntax especially for anonymous object instantiation, javax.swing components, Runnables, multithreading, etc., or is it general use?

Q4. Another version of this code uses more familiar syntax. Are these statements syntactically equivalent to the ones above (notwithstanding the different constructor call and event behavior)?

javax.swing.SwingUtilities.invokeLater(
    new Runnable(){
        public void run(){
            new MyClass(a,m);
        }
    }
);

and

javax.swing.myJButton.addActionListener(
    new ActionListener(){
        public void actionPerformed(ActionEvent e){
            myJTextField.grabFocus();
        }
    }
);

Solution

  • That is simply invalid Java syntax. You can confirm this for yourself by reading the JLS.

    • At that position, an <identifier> is required.
    • An <identifier> cannot start with a digit.

    So, basically, the stuff you are trying to maintain is not valid Java source code.

    My guess is that this is output from a decompiler, and the decompiler has encountered some bytecodes that it doesn't know how to decompile to Java. If you are "maintaining" decompiled code .... good luck to you! You will need to figure out what the code means from the context and / or by reverse engineering the bytecodes the hard way.