0

In Android there is a undeclared (hidden) class named "android.graphics.FontFamily" and I want to create static array of it. Any thing like this:

Class<?> clazz = Class.forName("android.graphics.FontFamily"); // Ok.
Class<?> clazz_array = Class.forName("android.graphics.FontFamily[]"); // Method threw 'java.lang.ClassNotFoundException' exception.

To build this hypothetical code:

FontFamily[] families = {fontFamily};

How can I do it?

Thankyou.

4
  • What does "undeclared (hidden) class" means? It means private, nested static private or inner private? Commented Jun 5, 2016 at 8:57
  • 1
    see Array.newInstance, documentation Commented Jun 5, 2016 at 8:58
  • @pskink If I understood well the array isn't declared in that class, he simply wants to create an array of that private class Commented Jun 5, 2016 at 8:59
  • 1
    @Fondesa, yes, he simply wants to create an array of that hidden class Commented Jun 5, 2016 at 9:00

2 Answers 2

3

You should be able to create an array reflectively then get its class. Example:

Class <?> clazz = Class.forName("android.graphics.FontFamily");
Object fontFamily = clazz.newInstance();
Object families = Array.newInstance(clazz, 1);
Array.set(families, 0, fontFamily);
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, clazzArray is class, and how can I instantiante an instance of it which has 1 item (like FontFamily[] families = {fontFamily})?
@mhtaqia see java.lang.reflect.Array documentation
1

You should specify the fully-qualified name as specified in the Class documentation. So in your case it would be:

Class<?> clazzArray = Class.forName("[Landroid.graphics.FontFamily;");

The [ indicates an array, and then the L prefix and semi-colon suffix are to indicate that the part in the middle is a class name.

Comments

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.