0

Is there any way to make a reference variable to the name of a class?

For example, let's say we have these two classes

class ClassA {
    public static String s = "This is class A";
}

class ClassB {
    public static String s = "This is class B";
}

and then we want to use them like this

// first somehow give the name of ClassA to some variable MyRef

// then print "This is class A"
System.out.print(MyRef.s); 

// then give the name of ClassB to the same variable MyRef

// and then print "This is class B"
System.out.print(MyRef.s);

Is there any way to do this? If not, is there any other simple way to do the same thing, that is, to easily switch between the static variables of ClassA and the static variables of ClassB?

4
  • simply ClassA.s and ClassB.s Commented Dec 31, 2014 at 9:47
  • 4
    ClassA.class.getName() Commented Dec 31, 2014 at 9:48
  • 1
    so what is wrong with ClassA.s? Commented Dec 31, 2014 at 9:49
  • 1
    Side-note: you don't need a semi-colon after a class body, and it looks unidiomatic to have it. Commented Dec 31, 2014 at 9:52

4 Answers 4

2

The closest you could come would be to pass ClassA.class or ClassB.class as a Class and then use reflection to get the field for that class, and its value.

// TODO: Much better exception handling :) (It's almost never appropriate to
// throw Exception...)
public static String getS(Class<?> clazz) throws Exception {
    Field field = clazz.getField("s");
    return (String) field.get(null);
}

Call it with:

String a = getS(ClassA.class);
String b = getS(ClassB.class);

Generally speaking, a better solution would be to use inheritance, but that requires an instance of some superclass or interface, and then a method instead of a field. Of course, that may not fit in with the rest of your requirements - we don't have much idea about the bigger picture of what you're trying to do.

Sign up to request clarification or add additional context in comments.

Comments

1

Try using interface, a way around:

interface MyRef {
    String getS();
}

class ClassA implements MyRef {
    public static String s = "This is class A";
    @Override
    public String getS(){
        return s;
    }
}

class ClassB implements MyRef {
    public static String s = "This is class B";
    @Override
    public String getS(){
        return s;
    }
}

class Ideone{
    public static void main (String[] args){
        MyRef ref = new ClassA();
        System.out.println(ref.getS());//prints: This is class A

        ref = new ClassB();
        System.out.println(ref.getS());//prints: This is class B
    }
}

Comments

0

Is this what you are looking for ?

public class MyRef {
    private Class<?> klass;
    public MyRef() {
    }
    public void setRef(Class<?> klass){
        this.klass = klass;
    }
    @Override
    public String toString() {
        return "This is " + klass.getName();
    }

    public static void main(String[] args) {
        class Test1{}
        class Test2{}

        MyRef mr = new MyRef();

        Test1 t = new Test1();      
        mr.setRef(t.getClass());
        System.out.println(mr);

        mr.setRef(Test2.class);
        System.out.println(mr);

    }

}

This prints out:

This is MyRef$1Test1   
This is MyRef$1Test2

Comments

-1

It seems to me that what you need is a reference to the class of your object. With that you can access the name of your class simply by invoking the class.getSimpleName() method.

For example:

ClassA a = new ClassA();
ClassB b = new ClassB();
System.out.println(b.getClass.getSimpleName());
System.out.println(b.getClass.getSimpleName());

This technique, in contrast to yours, doesn't require you to manually update the s field should you ever change the class name after a refactoring.

For more info on the class object see this: http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html

2 Comments

The question is about accessing a property of the class.
I'm sorry, but I don't think your downvote is fair. This solution seems to fit the usage scenario shown in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.