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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:31:10+00:00 2026-06-04T23:31:10+00:00

I have Java-related question: I want to know is there a way to create

  • 0

I have Java-related question:

I want to know is there a way to create path to class (in program) by using a variable(s).
Im making a program that will download pictures from certain sites and show them to a user. However, different sites have different forms, that’s why I have to define a series of functions specific to each. They cannot be put in the same class because functions that preform same job (just for another site) would have to have same names. I’m trying to make adding support for another site later as simple as possible.

Anyway, the question is, could I call a function in program using a variable to determine its location.

For example: code.picturesite.functionINeed();

code is the package containing all of the coding, and picturesite is not a class but rather a variable containing the name of the desired class – that way I can only change value of the variable to call a different function (or the same function in a different class).

I don’t really expect that to be possible (this was more for you to understand the nature of the problem), but is there another way to do what I’m trying to achieve here?

  • 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-06-04T23:31:12+00:00Added an answer on June 4, 2026 at 11:31 pm

    Yes, there is a way. It’s called reflection.

    Given a String containing the class name, you can get an instance like this:

    Class<?> c = Class.forName("com.foo.SomeClass");
    Object o = c.newInstance(); // assuming there's a default constructor
    

    If there isn’t a default constructor, you can get a reference to one via c.getConstructor(param1.getClass(), param2.getClass(), etc)

    Given a String containing the method name and an instance, you can invoke that method like this:

    Method m = o.getClass().getMethod("someMethod", param1.getClass(), param2.getClass(), etc);
    Object result = m.invoke(o, param1, param2, etc);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a simple web-service using Java. i want to load jars related
i found many related questions here. I have a simple java program . it
I have two questions related to the performance of Dozer using its Java API:
I have a question regarding class design. I want to have a class that
This maybe a related question: Java assignment issues - Is this atomic? I have
New to java and have a question related to packages. I like to keep
Is there a way to deploy a Java program in a format that is
(Related question: Java compilers or JVM languages that support goto? ) I have decompiled
question related to this I have a string a\;b\\;c;d which in Java looks like
There are 2 parts to my question but both are related. I have searched

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.