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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:52:47+00:00 2026-05-25T02:52:47+00:00

I have seen API’s designed where they at times accept class types to be

  • 0

I have seen API’s designed where they at times accept class types to be passed as parameter’s.
For eg[a]:

class AClass {
//details
}

class Someclass {

 public void someMethod(class klass, int num ) {
 // some code
 }

}

class client {

public static void main(String[] args) {
   Someclass obj = new Someclass();
   obj.someMethod(AClass.class, 10); // some sample use cases of client
}

}

Now in java, passing of class is not at all required if its possible to get the class type from the object, as explained below[b]

class AClass {
//details
}

class Someclass {

 public void someMethod(AClass obj, int num ) {
 // some code
 }

}

class client {

public static void main(String[] args) {
   Someclass obj = new Someclass();
   AClass aclassObj = new Aclass();
   obj.someMethod(aclassObj, 10); 
}

}

I can also change the parameter of someMethod() and use as given below [c]

public void someMethod(Object obj, int num ) {

 if(obj instanceof AClass) {
  //do something 
 }

}

So when should we choose the design as shown in [a] instead of [b] ? Are there any general principles to consider for design shown in [a].

PS: Some API’s designed like that are shown below

Quartz example

  • 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-25T02:52:48+00:00Added an answer on May 25, 2026 at 2:52 am

    A couple of obvious answers are:

    1. Any time the method doesn’t actually take the object as a parameter (e.g. when you request an instance of a type from a DI framework or factory, such as in the Quartz example you provided).
    2. Any time the parameter you pass in might be null, so you cannot call getType on it, and instanceof won’t indicate that the object is an instanceof any type.

    So it makes sense to take a class argument when the method in question either needs to produce an instance of the class in question or provide some metadata about that class which shouldn’t require an instance to be created.

    The only other consideration I’d point out is that it is possible to use generics when accepting a class parameter, as in the following method signature on Guice’s Injector interface:

    <T> T getInstance(Class<T> type);
    

    Notice how this allows the method to declare that it’s returning an object of the same type that you pass in as a class parameter, so you don’t need to cast the object afterward. You can also use the generic wildcards (Class<? extends Foo>) to restrict the types of classes that can be passed into the method.

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

Sidebar

Related Questions

I have seen the API demos of android (/ApiDemos/src/com/example/android/apis/view/Gallery1.java) , but they take images
Is there a twitter API that returns mood? I have seen some websites that
Any good CCK API docs out there? I have seen http://api.audean.com/ , but can't
I have seen VS2010 exposing Network Emulator API. I have installed it and trying
I have seen a lot of questions here regarding the Facebook Graph API but
I have seen this question in SO several times. Most of cases user called
I have seen facebook javascript api mentioning that loading the sdk asynchronously // Load
I want to load activity with flip animation. I have already seen api demos
I have been looking at the Windows API Code Pack 1.1 and have seen
I have seen something really cool at Yippy They are loading the url in

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.