reflection « Static Class « Java Class Q&A

Java Class Q&A
1.abstract class
2.Base class
3.class hierarchy
4.class name
5.class version
14.equal method
15.extend Class
19.inner class
21.main class
26.object reference
28.parent class
31.Private Field
35.Static Class
39.Wrapper Class
Java Class Q&A » Static Class » reflection 

1. Which class invoked my static method?

Suppose that I have a Java class with a static method, like so:

class A
    static void foo()

2. Java: How to get a class object of the current class from a static context?

I have a logging function that takes the calling object as a parameter. I then call getClass().getSimpleName() on it so that I can easily get the class name to add ...

3. Getting value of public static final field/property of a class in Java via reflection

Say I have a class:

public class R {
    public static final int _1st = 0x334455;
How can I get the value of the field/property "_1st" via reflection?

4. How do I access a JavaFX 1.3 static class member from Java?

I want to access a static JavaFX class member from Java using the Javafx reflection API. E.g. JavaFX code:

public var thing;

class MyJavaFXClass {
Java code:
private Object getThing() {
  FXClassType classType = FXContext.getInstance().findClass("mypackage.MyJavaFXClass");


5. Java - Get reference to a static class using reflection

In Java, is it possible to access an instance of a static class (nested) using reflection? Supposing I have the following 2 classes defined in the package Package1.SubPackage.SubSubPackage:

public class MyMainClass {  ...

6. Retrieve only static fields declared in Java class

I have the following class:

public class Test {
    public static int a = 0;
    public int b = 1;
Is it possible to use reflection to ...

7. getDeclaredMethods() and hidden super class static method

According to, Class.getDeclaredMethods() should only include methods "declared by the class". However, I am getting some pretty surprising results with the test code below:

import java.util.Arrays;

class A {
 public ...

8. Java: static nested classes and reflection: $ vs

If I have a class com.example.test.Enum2.Test as in the code below, why does getCanonicalName() return com.example.test.Enum2.Test but Class.forName() requires "com.example.test.Enum2$Test" as an argument? Is there a way to be consistent, so that ...

9. How to access a static method via a class reference

class A {
    public static void foo() {}

class B {
    public static void foo() {}
I have Class clazz = A.class; or B.class; How do I access ...

10. Multiple instances of static variables

I'm experimenting with using different classloaders to load a particular class, and see if the static variables in that class can have different instances. Basically, I'm trying to write code for ...

11. Get value of final field of a static class using reflection

I have following class structure:

       private static class MyStaticClass {  
          public final String name; ...

12. Custom class loader for static classes without reflection

Not a very nice singleton - Singletons should have private constructors, and why have a seperate loaded flag? No, clearly you cannot invoke static methods like getInstance() in a dynamically loaded class except through reflection. Writing custom classloaders is neither here nor there (and URLClassLoader will almost always do the job anyway).

13. Enum values require reflection - should have static values( Class ) method

YourEnum.values() works for a specific Enum type. It doesn't work when you're dealing with a class object. Instances come up all the time where you're writing a class that works for any type of Enum. So your class has a generic type > and a reference to the enum class object, Class theEnumClass. Now how do you get at ...  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.