How can I create a HashMap that dynamically calls other Java classes?

SheldonFan

I am attempting to create a HashMap<Integer, Class>, and am not being successful. Essentially, all I want to do is have the ability to dynamically load the classes into the Map.

My managed Bean looks like this:

package Demo;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.Dependent;
import javax.inject.Named;

/**
 *
 * @author kbarnett
 */
@Named(value = "facePalmBean")
@Dependent
public class FacePalmBean {

    private HashMap<Integer, Class> chimpanzee;
    private NewClass0 NewClass0;
    private NewClass1 NewClass1;
    private NewClass2 NewClass2;

    /**
     * Creates a new instance of FacePalmBean
     */
    public FacePalmBean() {

        chimpanzee = new HashMap<>();
        NewClass0 = new NewClass0(0);
        NewClass1 = new NewClass1(1);
        NewClass2 = new NewClass2(2);

    }

    public HashMap<Integer, Class> getChimpanzee() {
        for (int i = 0; i < 3; i++) {
            try {
                String tmpstring = "NewClass"+i;
                System.out.println(tmpstring);
                Class tmpclass = Class.forName(tmpstring);
                System.out.println(tmpclass);
                chimpanzee.put(i, tmpclass);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(FacePalmBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        System.out.println(chimpanzee.toString());
        return chimpanzee;
    }

    public void setChimpanzee(HashMap<Integer,Class> chimpanzee) {
        this.chimpanzee=chimpanzee;
    }


}

and the NewClasses look like this:

package Demo;


public class NewClass0 {
        Integer MyNumber;

    NewClass0(int num){
        MyNumber=num;
    }

    public Integer getMyNumber() {
        return MyNumber;
    }
}

All of the NewClasses are identical except for the number (i.e. 0, 1, and 2).

Chris Thompson

In order to load a class with the Class.forName() method, you must specify a fully qualified package name. In this case it must be Demo.NewClass0, for example.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I chain functional calls in Java?

From Java

How can I dynamically create class methods for a class in python

From Dev

Why can I not create a HashMap with 'long' types in Java?

From Dev

How Can I create a generic HashMap to insert collections and objects?

From Dev

How can I dynamically create setup file based on unique identifier?

From Dev

How can I create a dynamically expanding array of arrays?

From Dev

How can I dynamically create rows in Jade?

From Dev

Can I create Domain classes in separate Java Project and import in Grails

From Dev

Why can I not use the public methods from other classes on an object that I got from a HashMap?

From Dev

How can I create multiple setInterval()s dynamically?

From Dev

How can I create an array of classes in Swift

From Dev

How can I dynamically create input elements with different Ids?

From Dev

How can I dynamically create a php variable in a PhpStorm live template?

From Dev

How can I dynamically create a checkerboard using jQuery

From Dev

How can i create dynamically tab with viewpager in android?

From Dev

Can I swap HashMap keys and values to other?

From Dev

How can I chain functional calls in Java?

From Dev

How can i create dynamically tab with viewpager in android?

From Dev

How can I assert on a Java Hashmap of Arrays?

From Dev

In AngularJS how can I create a new ngController dynamically?

From Dev

How can I set a Java-like "static variable" in Perl that can be accessed by other classes ?

From Dev

How can i show dialog boxes after each other dynamically?

From Dev

Why can I not use the public methods from other classes on an object that I got from a HashMap?

From Dev

How can I directly access methods from my library class in other classes? Java

From Dev

How can I create a List<ViewModel> with 2 other classes of models? MVC ASP.NET

From Dev

How do I scan items from other classes in java?

From Dev

How can I center div, inside other div with bootstrap classes?

From Dev

How do I dynamically create instances of all classes in a directory with python?

From Dev

I want to create Jmeter script in which I can provide delay between dependent API calls dynamically

Related Related

  1. 1

    How can I chain functional calls in Java?

  2. 2

    How can I dynamically create class methods for a class in python

  3. 3

    Why can I not create a HashMap with 'long' types in Java?

  4. 4

    How Can I create a generic HashMap to insert collections and objects?

  5. 5

    How can I dynamically create setup file based on unique identifier?

  6. 6

    How can I create a dynamically expanding array of arrays?

  7. 7

    How can I dynamically create rows in Jade?

  8. 8

    Can I create Domain classes in separate Java Project and import in Grails

  9. 9

    Why can I not use the public methods from other classes on an object that I got from a HashMap?

  10. 10

    How can I create multiple setInterval()s dynamically?

  11. 11

    How can I create an array of classes in Swift

  12. 12

    How can I dynamically create input elements with different Ids?

  13. 13

    How can I dynamically create a php variable in a PhpStorm live template?

  14. 14

    How can I dynamically create a checkerboard using jQuery

  15. 15

    How can i create dynamically tab with viewpager in android?

  16. 16

    Can I swap HashMap keys and values to other?

  17. 17

    How can I chain functional calls in Java?

  18. 18

    How can i create dynamically tab with viewpager in android?

  19. 19

    How can I assert on a Java Hashmap of Arrays?

  20. 20

    In AngularJS how can I create a new ngController dynamically?

  21. 21

    How can I set a Java-like "static variable" in Perl that can be accessed by other classes ?

  22. 22

    How can i show dialog boxes after each other dynamically?

  23. 23

    Why can I not use the public methods from other classes on an object that I got from a HashMap?

  24. 24

    How can I directly access methods from my library class in other classes? Java

  25. 25

    How can I create a List<ViewModel> with 2 other classes of models? MVC ASP.NET

  26. 26

    How do I scan items from other classes in java?

  27. 27

    How can I center div, inside other div with bootstrap classes?

  28. 28

    How do I dynamically create instances of all classes in a directory with python?

  29. 29

    I want to create Jmeter script in which I can provide delay between dependent API calls dynamically

HotTag

Archive