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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:51:59+00:00 2026-05-16T02:51:59+00:00

By default, C++ will do auto promotion in assignment if appropriate constructors exist (and

  • 0

By default, C++ will do “auto promotion” in assignment if appropriate constructors exist (and are not declared explicit).

In Java, this behavior doesn’t happen by default. If I want automatic promotion, is there a way to declare my constructors as implicit?

For example, here is some C++ code that has the effect that I want:

class Foo {
    public:
        Foo(string) { /* ... */ }
        /* Foo's methods and stuff */
};

void DoSomethingWithAFoo(Foo foo)
{
    /* ... */
}

int main()
{
    string s = "I am a happy string, I swear!";

    DoSomethingWithAFoo(s);    

    return 0;
}

Generally, in C++ this is allowed, and the string s will be automatically promoted (by constructing a temporary Foo from s).
Since the Foo(string) constructor is not marked explicit, I don’t even need a typecast.

Is there a way to do this in Java?

I ask because I am trying to create a specific type that represents any one of a specific variety of other types. For example, imagine a class Primitive that was representing either a single boolean, integer, character, or double (that’s not my specific example, but it is related).

Methods in my system that expect a Primitive should also accept any of the “real types” that the Primitive might represent (by auto-promotion to an anonymous object of type Primitive through a constructor call).

In my actual work (as opposed to this example), my equivalent to the Primitive class has constructors both from a few primitive types as well as several object types (each constructor taking only the one parameter). Ideally I would want auto promotion for all of them.

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

    No. Java doesn’t have implicit constructors.

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

Sidebar

Related Questions

By default it will not print out anything until the whole page has finished
My host will not allow me to change the default folder of my primary
The default behavior of XML serialization (to_xml) for ActiveRecord objects will emit 'type' and
How can I create a table where the my :id field will not auto-increment?
The input element by default is size=20 if this is not defined inline or
for a menu checkItem, when user clicks at it, by default it will trigger
How do I set form number four as the default form that will run
I have an action bean in my stripes application. The default handler/method will display
which one will be better to use default magic quotes or user defined addslash/stripslash
Can we change default yellow focus that will appear when ListView items end while

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.