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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:44:09+00:00 2026-06-10T09:44:09+00:00

I know if I were in PHP I would be able to create an

  • 0

I know if I were in PHP I would be able to create an object as following:

// class Name
$var = "Name";
$my_name = new $var("John", "Doe");

Now, I wanted to adopt similar approach in Java. I have a file which I need to parse and produce objects depending on the line read from the file. For example:

2235:org.powertac.common.TariffSpecification::8::addRate::9
2235:org.powertac.common.Tariff::8::new::8
2235:org.powertac.common.TariffSpecification::10::new::1::PRODUCTION
2236:org.powertac.common.Rate::11::new
2236:org.powertac.common.Rate::11::withValue::0.015
2236:org.powertac.common.Rate::11::setTariffId::10
2236:org.powertac.common.TariffSpecification::10::addRate::11
2236:org.powertac.common.Tariff::10::new::10
2247:org.powertac.common.RandomSeed::12::init::DistributionUtilityService::0::model::-7651755067434358
2257:org.powertac.genco.Genco::13::new::nsp1a::true::true
2258:org.powertac.genco.Genco::13::new::nsp1a
2258:org.powertac.genco.Genco::15::new::nsp1b::true::true

then I should create an object of the type Rate, (new), with default values.
Therefore, parsing that line would yield me:
"org.powertac.common.Rate"(String) and I wanted to create a org.powertac.common.Rate object. How could I do it? I know it’s a lazy evaluation … how can I create something similar in Java?

  • 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-10T09:44:10+00:00Added an answer on June 10, 2026 at 9:44 am

    Use the Java Reflection API:

    String className = "org.powertac.common.Rate";
    String ctorArg = "8";
    Class<Rate> clazz = (Class<Rate>) Class.forName(className);
    Constructor<Rate> ctor = clazz.getConstructor(String.class);
    Rate rate = ctor.newInstance(ctorArg);
    

    Then, if you want to invoke a method using Reflection:

    String methodName = "setTariffId";
    String methodArg = "1";
    Method setTariffId = clazz.getMethod(methodName, String.class);
    setTariffId.invoke(rate, methodArg);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if PHP is Object Oriented, I read that from
I would like to know how to create a php function that can be
Anyone know how I would be able to reduce my following lines of code?
I wanted to know how PHP would execute this. Order of operations addslashes(strip_tags($record['value'])); Is
I know how I would achieve this using PHP, but with jQuery / JavaScript
I would like to know what's the best way to organize my php project
I recently finished my PHP app and i would like to know if there's
Ok well heres what I would like to do in PHP: http://www.wordle.net/ I know
I'm using a multi dimensional array in php, I would like to know how
I know that I can create an associative array like this: var MyAssocArray =

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.