New Keywords in Java 9

后端 未结 5 1194
無奈伤痛
無奈伤痛 2021-01-31 07:53

One of Java 9\'s largest features will be a module system defined by Project Jigsaw. When reading slides from the Project Jigsaw: Under the Hood at JavaOne 2015, I noticed the f

5条回答
  •  再見小時候
    2021-01-31 08:32

    moduleis a new keyword introduced to define the interdependencies between packages. Why we need modules? Because earlier

    1. the encapsulation was not immaculate. With the help of reflection and with similar techniques we could access even the private field.

    2. All classes in all jars were publicly accessible.

    3. If the Classloader does not get the class it had to look into a lot of areas and load a lot of related files and even after that if the Class is not found, it would throw NoClassDefFoundErrors at run time.

      So for all the above reasons we need a mechanism which lets JVM to know this at runtime. For implementing module, you need to define module-info.java. in that package

          module com.exporter{
      
           exports com.a;
           provides com.b.M;
            with com.b.MImpl; }
      

    In some other package,

    module com.consume {
        requires com.a;
    }
    

    Other attributes used are "exports" and "requires" for making an inter dependency(transitive dependency only), "provides" and "with" for exposing interface and mentioning implementation. So it a strong encapsulation, may be, that is why java 9 is more inclined to better Object Oriented feature.

提交回复
热议问题