Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7024905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:54:14+00:00 2026-05-27T23:54:14+00:00

How do I instantiate an object using the Class class? I would like to

  • 0

How do I instantiate an object using the Class class?

I would like to pass a Class object to a function and have that function return a new object of the class I pass in.

I will be doing casting after I get the object back. Also, are there any suggestions on design patterns for this sort of thing?

Here is some example pseudo code that shows what I want to do:

Class Animal {
    String name;
}

Class Cat extends Animal {
    boolean hasTail;
}

MAIN:

Class myClass = Cat.class;
Animal createAnimal(myClass) {
    return new myClass;
}

Edit:

Also, is there a way to cast within my function before returning the new instance or am I stuck with the default Object type?

return (myClass)myClass.newInstance();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:54:14+00:00Added an answer on May 27, 2026 at 11:54 pm

    I will be doing casting after I get the object back.

    Also, is there a way to cast within my function before returning the new instance or am I stuck with the default Object type?

    You don’t need to do casting if you take advantage of generics. Generics are described in relatively introductory detail here

    Here is a java example derived from your psuedo code using generics and reflection:

    public class ReflectionNewInstanceTest {
    
        static class Animal {
            String name;
        }
    
        static class Cat extends Animal { 
            boolean hasTail = true;
        }
    
        public static void main(String[] args) throws Exception {
            Animal a = createAnimal(Cat.class);
        }
    
    
        static <T extends Animal> T createAnimal(Class<T> clazz) throws Exception {
            return clazz.newInstance();
        }
    
    }
    

    As for patterns, any of the typical creational patterns should be looked at when you want to abstract away the creation of objects. The simplest is probably just using a factory method. The example I have above is just a general purpose factory method that doesn’t really gain you much. Here is a pretty short list of patterns you should look up:

    • Abstract factory pattern
    • Factory method pattern
    • Builder pattern
    • Prototype pattern
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to instantiate an object from its Class object, using the constructor
In .Net, suppose I instantiate an object that starts an asynchronous process (using Delegate.BeginInvoke
Using the C# object initializer syntax I can instantiate an anonymous object like this:
Let's say I instantiate an object in Javascript like this: var myObj = new
In PHP, if you define a class, and then instantiate an object of that
I have a template method that needs to instantiate class objects with given class
I am using an abstract factory to return instances of concrete subclasses.I would like
Would like to create a method that instantiate objects. - (NSArray *) make3Of :
I would like to write a class that would contain all my MySQL operations.
When you instantiate a new object by calling constructor, i.e. Foo bar = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.