Skip to content

Instantly share code, notes, and snippets.

@aman-junaid
Last active October 3, 2022 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aman-junaid/b7b277ea6aeee054ed27b4a42c3a7073 to your computer and use it in GitHub Desktop.
Save aman-junaid/b7b277ea6aeee054ed27b4a42c3a7073 to your computer and use it in GitHub Desktop.
package com.aj.snippet.javamodule.a;
// default access modifier
class A {
}
package com.aj.snippet.javamodule.a;
public class B {
}
package com.aj.snippet.javamodule.b;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.aj.snippet.javamodule.a.A;
//This is not allowed as A has default access modifier and can be accessed within package
import com.aj.snippet.javamodule.a.B;
//This is allowed as B has public access modifier
public class Z {
void testMethod() {
try {
Class x = Class.forName("com.aj.snippet.javamodule.a.A");
// But we can access the class A using reflection
Field[] fields = x.getFields();
Method[] methods = x.getMethods();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@aman-junaid
Copy link
Author

Demonstrating the issue with the non-modular approach of Java packaging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment