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 6714637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:31:07+00:00 2026-05-26T08:31:07+00:00

In Java we can instantiate an object (knowing only the parameters types) dynamically like

  • 0

In Java we can instantiate an object (knowing only the parameters types) dynamically like this:

Class<?> msgClass = Class.forName(className);
constructor = msgClass.getConstructor(String.class, String.class, String.class);    
MyClass myClass = (MyClass) constructor.newInstance(myString, myString2, myString3);    

    public class MyClass {        
        public MyClass(String s, String s2, String s2){}
    }

Is there a way to do the same in objective-c, knowing only the parameters types to be passed in to the objective-c class constructor.

Thanks in advance.

  • 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-26T08:31:08+00:00Added an answer on May 26, 2026 at 8:31 am

    Yes, depending on what you’re really doing. ObjC doesn’t have language-level “constructors” in the same way– there are init methods which usually called alongside the alloc instantiation method, but note that init is there by framework convention and not by spec, so the runtime doesn’t “know” what “constructor” you’d want to call.

    You can instantiate an object (the equivalent of allocating it) like this:

    id myObj = class_createInstance(NSClassFromString(@"MyClass"));
    

    although you might as well do it more directly:

    id myObj = [NSClassFromString(@"MyClass") alloc];
    

    But then you’d still need to call whatever init method you want on it. If you don’t want to do this directly, you need to know the selector for the method so you can send the object the correct message. The selector is a static representation of the message signature. You could invoke that method through the runtime API like this:

    myObj = objc_msgSend(myObj, @selector(initWithStr1:str2:str3), myString1, myString2, myString3);
    // Check myObj for nil which means a failed init.
    

    ObjC runtime reference: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In java, can I use a class object to dynamically instantiate classes of that
I'd like to use define a generic class in java, that can only be
In java, I can write code like this Boolean b = true ; And
How can I instantiate a JMS queue listener in java (JRE /JDK / J2EE
How can you instantiate a Bimap of Google-collections? I've read the question Java: Instantiate
What is the maximum file size 32 bit java can access? Is this architecture
Maybe the C++ and Java people can help me to define this problem I'm
When you instantiate an object, why do you specify the class twice? OddEven number
So I come from a background in Java where you can create a class
In Java I can do something like the following: TreeMap<Double, String> myMap = 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.