There is a certain class Contakt.class , but the names do not matter as there are many classes, the point is that instead of Contakt you need to substitute other values ​​dynamically.

I get the text data and insert it into the String q2=text; then I have code where Contakt.class , but instead of Contakt you need to insert q2 , where the names of other classes are stored.

Something will q2.class like q2.class , where instead of q2 will be various values.

Is there such an option in Java?

  • Sorry, but what is the relationship between the title and the question? In a question you mean modular loading of classes? This is not badly described in the following articles: habrahabr.ru/post/103830 habrahabr.ru/post/104229 - Pogromist engineer
  • I do not know how to head a question, at first I thought that you can just remove the quotes from the String and you can insert it. Already realized that no - Artsait
  • 2
    Ideally, you need Class.forName (), take a look at tutorialspoint.com/java/lang/class_forname.htm . - Kirill Stoianov
  • @KirillStoianov, issue your comment as an answer, add a simple example, and you will have a reputation! - Vladyslav Matviienko
  • Thank you all for the help, but I realized that this is not what I was looking for. Now I have created a new question - Artsait

2 answers 2

Using the Class.forName() and newInstance() methods of the Class object, you can dynamically load and create instances of a class when the class name is unknown until the program runs.

 Class c = Class.forName("com.xyz.MyClass"); Object obj = c.newInstance(); MyClass myClass = (MyClass) obj; 

I found the solutions, but this is certainly not what was needed

  try { // returns the Class object for the class with the specified name cls = Class.forName("net.artsait.bukvitsa."+bukvica); name100 = cls.getSimpleName(); } catch(ClassNotFoundException ex) { System.out.println(ex.toString()); } 

Now I wrote a new question

Android Dynamically change class names