static final String PACKAGE_NAME = MyClass.class.getName().substring(0, MyClass.class.getName().lastIndexOf('.'));
2) I don't see any problems with different class floating around, because this snippet stores the package name which is obviously the same.
Anyway, it would be great if somebody provide a better solution
The most common need for dealing with class and package names as literal strings is for loading resources from the classpath. I avoid this by making sure there's a marker class I can use in the package I'm loading the resource from and loading relative to that class.
You need to create an account or log in to post comments to this site.
Sometimes I hate Java.