Determining Fully Qualified Class Name from Java.lang.String

13.8k Views Asked by At

I have considered in thought how I can take a java.lang.String representing a class name and determine the fully qualified name to then convert the string to a class using Java.lang.Class.forName(FullyQualifiedName).

This is puzzling me because I can't use Java.lang.Class.forName() with a Java.lang.String representing a class name such as "Address" because I need the fully qualified name or I get an Exception.ClassNotFoundException which cycles me back to needing to know the fully qualified name but how when I can't use Java.lang.Class.forName().

I am simply stuck with my current knowledge. I have searched using Google and even with the creation of this question but I bet this has already been asked-n-answered. Someone please point me in the right direction?

Note: This question should be removed as it does not appear possible to do what the OP is requesting in Java.

1

There are 1 best solutions below

6
On

The fully qualified name of a class is the name of the class prefixed with the package name. For example if class Address is in a package com.mycompany.myproject, then the fully qualified name of class Address is com.mycompany.myproject.Address.

Java can only find classes by their fully qualified name (unless the class is in the default package - but then the simple name of the class is also the fully qualified name).

You need to know in what package your class is in, otherwise there is no way to load the class with Class.forName(...).