Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Base
- {
- static void foo()
- {
- // I want to get Derived class here
- // Derived.class
- }
- }
- class Derived extends Base
- {
- }
- Derived.foo();
- class Base
- {
- static void foo(Class< T > calledBy)
- {
- // I want to get Derived class here
- // Derived.class
- }
- }
- class Derived extends Base
- {
- }
- Derived.foo(Derived.class);
- class Base {
- static void foo() {
- // Only the static context is available here so you can't get class dynamic class information
- }
- void bar() {
- System.out.println(getClass());
- }
- }
- class Derived extends Base {
- }
- class Another extends Base {
- static void foo() {
- // No super call possible!
- // This method hides the static method in the super class, it does not override it.
- }
- void bar() {
- super.bar();
- }
- }
- Derived derived = new Derived();
- derived.bar(); // "class Derived"
- Base base = new Base();
- base.bar(); // "class Base"
- // These are only "shortcuts" for Base.foo() that all work...
- derived.foo(); // non-static context
- Derived.foo(); // could be hidden by a method with same signature in Derived
- base.foo(); // non-static context
- Base.foo(); // Correct way to call the method
- Another a = new Another();
- a.foo(); // non-static context
- Another.foo();
- protected static Class<?> getModelClass(int dept) throws ClassNotFoundException {
- Class<?> clazz = null;
- try {
- throw new Exception("dummy");
- } catch (Exception ex) {
- StackTraceElement[] trace = ex.getStackTrace();
- StackTraceElement e = trace[dept];
- String name = e.getClassName();
- clazz = Class.forName(name);
- }
- return clazz;
- }
- Derived.foo();
- class Base {
- static void foo() {
- System.out.println("Base");
- }
- }
- class Der extends Base {
- static void foo() {
- System.out.println("Der");
- }
- }
- class Check {
- public static void main(String[] args) {
- Base.foo();
- Der.foo();
- }
- }
- javac -g Check.java && java Check
- Base
- Der
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement