Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hello! I'm trying to create a chunk of java which allows me to call classes depending on strings called from an array - however, I can't seem to get it to work, unfortunately. If anyone could help I'd be most grateful, I've included the code I've hashed out so far...

public class ProcrastinatorPanel extends JPanel {

    String[] room = {
             "Hallway", "Bathroom", "Study", 
             "Bedroom", "Dining Room", "Living Room", 
             "Kitchen", "Playroom", "Bathroom", "Garage" };

    int roomNumber = 0;

    public ProcrastinatorPanel() {

        Hallway.hallway = new Hallway();
        Class roomClass = Class.forName(room[roomNumber]);
    }
}
share|improve this question
3  
Do you have a class called Hallway? –  Thilo Jun 12 '13 at 18:57
1  
You have no class named Hallway that the classloader can find (which in this case means it's not in the default package). Use the fully qualified name. –  Brian Roach Jun 12 '13 at 18:58
    
This is what's bothering me - the class named Hallway exists, and is in the same source folder... –  user2479645 Jun 12 '13 at 18:58
1  
Hallway needs to be in the discouraged default package. Or you need to specify a package name. –  Thilo Jun 12 '13 at 18:59
1  
@ProfessionalAmateur Not necessarily, hallway could be a static field of Hallway. –  arshajii Jun 12 '13 at 19:00

2 Answers 2

up vote 2 down vote accepted

For the Class.forName() method to work you need to pass it a string representing a fully qualified Class name. E.g. "com.mycompany.main.Hallway"

You could also try making an array of Classes instead of an array of strings. To get you started, the expression Hallway.class will evaluate to the Hallway class

Also as professionalamateur pointed out Hallway.hallway = new Hallway() is not going to work. The code should read Hallway hallway = new Hallway(). Hallway is the type of the variable and hallway is the name.

share|improve this answer
    
+1 for making an array of Class. –  Thilo Jun 12 '13 at 18:59
    
I didn't think it was possible to place classes into arrays - what would you define the type as? –  user2479645 Jun 12 '13 at 19:02
    
@user2479645 That's a good question, I never use arrays. Just Class I would imagine. I'm sure Class<?> wouldn't work. You could also of course declare a Set of classes (e.g. HashSet<Class> or HashSet<Class<?>>) or an ArrayList or something –  Accipheran Jun 12 '13 at 19:05
    
That seems to work - thank you ever so much! –  user2479645 Jun 12 '13 at 19:08
    
@user2479645 anytime –  Accipheran Jun 12 '13 at 19:09

Class.forName() would require full qualified class name.

Also I did not see any catch/throw of ClassNotFoundException exception. Is the code you mentioned complete?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.