Java classes: anonymous vs nested and private

Can someone explain the difference between anonymous classes, nested classes, and private classes in Java? I would like to know the runtime costs associated with each of them, and the approach to the compiler for each, so I can access which is best used, for example. performance (potential for compiler optimization), memory usage, and general acceptability with other Java codes.

By anonymous classes, I mean those that are declared inline in a function in this strange Java mode. This is often found in Swing code examples, where you write a button handler inside with a declaration. I donโ€™t like anonymous classes because you get these funny MyClass $ 1.class files, and most of the time you will have to reorganize later when it turns out that you want to make the anonymous class a single or if you need a handle or something. I also don't like the way you attach a handler in the form of something that you just created, while in the middle of another function call. It just makes me feel weird. :-)

Private classes are those that you write at the bottom of the file completely outside the declaration of your public class. I prefer private classes only because they make sense in other languages โ€‹โ€‹where there is no 1: 1 strong connection between the source file and the class. I donโ€™t feel this strange feeling, and I know (or, it seems to me, know) how the Java compiler will handle the declaration of this class.

Nested classes are those written inside the squigglies of your public class. I really don't know what a nested class means compared to the other two. :-)

+3
source share
3 answers

Can someone explain the difference between anonymous classes, nested classes, and private classes in Java?

  • Anonymous classes are e.g. Runnable in

    new Thread(new Runnable() { public void run() { ... } }.start(); 
  • Nested classes look like

     class SomeClass { ... class NestedClass { .... } } 
  • Private classes (which you call "the ones you write at the bottom of the file completely outside the declaration of your public class") are actually package-span classes. You cannot have a private modifier in front of a class if it is not nested!

I would like to know the runtime costs associated with each of them, and the compiler approach for each, so I can access which is best used, for example. performance (potential for compiler optimization), memory usage and general acceptability with other Java codes.

I doubt that there is a difference in performance between these approaches. What for? Because each approach ends up being compiled into a separate more or less โ€œnormalโ€ class. ("More or less" due to the fact that in some cases an access method is created, but it has a side effect and is most likely built-in JIT anyway.)

This code:

 public class TestOuter { int fieldOuter; public void methodOuter() { } class TestInner { int fieldInner; public void methodInner() { } } } class TestPackage { int fieldPackage; public void methodPackage() { } } 

It turns out compiled into:

 $ javap -c TestOuter$TestInner Compiled from "TestOuter.java" public class TestOuter extends java.lang.Object{ int fieldOuter; public TestOuter(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public void methodOuter(); Code: 0: return } 

 $ javap -c TestOuter Compiled from "TestOuter.java" public class TestOuter extends java.lang.Object{ int fieldOuter; public TestOuter(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public void methodOuter(); Code: 0: return } 

 $ javap -c TestPackage Compiled from "TestOuter.java" class TestPackage extends java.lang.Object{ int fieldPackage; TestPackage(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public void methodPackage(); Code: 0: return } 
+7
source

As regards runtime costs, private and public classes are the same. There is a difference in theory, because the JVM more easily embeds methods from a private class, but this theory.

Nested classes: There are two types, static and non-static. This is actually a relatively big difference: a static class is like a private class, and a non-static class has a pointer (reference, whatever you call it) to the parent object. This means that this requires more memory, so if possible, use static nested classes. Otherwise, nested classes will be as fast as private or public classes. From a non-static nested class, you can access the parent object (why does it have a reference to the parent), but be careful when accessing private fields, as this is done using special hidden anonymous functions (Eclipse will give you a warning).

If you really want to understand all the differences, you should use javap to decompile the class.

+3
source

Correct some terminology first.

  • A private class is a class declared using the private access modifier. This modifier can only be used for a nested class, so private classes are a subset of nested classes.

  • The classes you write at the bottom of the file completely outside the declaration of your public class are actually private. And this is because of their access / visibility ... not the fact that they are in one file of another class.

(Apart from the fact that you like to do this, it is generally accepted that the BAD IDEA puts more than one top-level class in one source file. It can break tools and IDEs to begin with: http://geosoft.no/development/javastyle. html .)

So what is the difference between the two?

  • A private class of a package is the same as the regular class public or protected , except that it is ONLY displayed by other classes in the same package.

  • Nested classes are a static class declared in another class. It behaves in all goals and objectives in the same way as a non-nested class. In particular, instances can be created independently of any instances of the closing class.

  • A local inner class is not a static class that is declared in another class. An instance must be created in the context of an instance of the enclosing class and can access all members of this instance.

  • An anonymous inner class is functionally similar to a local inner class, except that:

    • declared inside the method and can use the variables declared in the methods (provided that they are final ), and
    • he has no name and cannot be independently created.

    The anonymous internal class syntax combines declaration and instantiation and is designed to simplify writing lightweight callbacks.

What about performance?

  • The performance of a nested class (private or other) is identical to the regular class.
  • Two forms of the inner class have a tiny execution cost because they use a hidden attribute containing a reference to the enclosing instance.

How about acceptability?

  • Nested classes (including private ones) and two forms of inner classes are acceptable and use them. (Obviously, some of them are more appropriate than others, depending on the use case. But you can say this about every Java construct.)

  • A form that you incorrectly designate as "private" (where you put multiple top-level classes in a single source file) is generally considered a bad idea.

+1
source

Source: https://habr.com/ru/post/989578/


All Articles