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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:27:26+00:00 2026-06-12T10:27:26+00:00

In this code construct: public MyClass(Integer… numbers) { do_something_with(numbers[]); } is it possible to

  • 0

In this code construct:

public MyClass(Integer... numbers) {
    do_something_with(numbers[]);
}

is it possible to require that numbers contains at least one entry in such a way, that this is checked at compile-time? (At run-time, of course, I can just check numbers.length.)

Clearly I could do this:

public MyClass(Integer number, Integer... more_numbers) {
    do_something_with(number, more_numbers[]);
}

but this isn’t going to be very elegant.

The reason I would like to do this is to make sure that a sub-class does not simply forget to call this constructor at all, which will default to a call to super() with no numbers in the list. In this case, I would rather like to get the familiar error message: Implicit super constructor is undefined. Must explicitly invoke another constructor.

Could there be another way to achieve the same, like some @-annotation that marks this constructor as non-implicit?

  • 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-12T10:27:27+00:00Added an answer on June 12, 2026 at 10:27 am

    I suppose one incredibly hacky way to do this is to create a no-args method and mark it as deprecated. Then compile with these two flags: -Xlint:deprecation -Werror. This will cause any use of a deprecated method to be a compile time error.

    edit (a long time after the initial answer):

    A less hacky solution would be to ditch the MyClass(Integer... numbers) constructor and replace it with MyClass(Integer[] numbers) (and add a private no-args constructor). It stops the compiler from being able to implicitly use the super class constructor, but without any args, and gives you a compile time error message.

    ./some_package/Child.java:7: error: constructor Parent in class Parent cannot be applied to given types;
        public Child(Integer[] args) {
                                     ^
      required: Integer[]
      found: no arguments
      reason: actual and formal argument lists differ in length
    

    The cost is your calling syntax will become a bit more verbose:

    new Child(new Integer[] {1, 2, 3});
    

    You can of course write a helper functions to help with this eg.

    public static Child newInstance(Integer... numbers) {
        return new Child(numbers);
    }
    
    @SafeVarargs
    public static <T> T[] array(T... items) {
        return items;
    }
    

    and then:

    Child c0 = Child.newInstance(1, 2, 3);
    Child c1 = new Child(array(1, 2, 3));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this code : public class MyClass { private Object innerValue; public Object getInnerValue()
I have this code to get the default constructor: Public Function f(ByVal t As
Can you please tell me what kind of construct in C# is this. Code
The other day, I came across this construct: static_cast<size_type>(-1) in some example C++ code,
With the code: template <typename T> class MyClass { public: MyClass(const MyClass& other) {
Say I have the following class: public class MyClass<T> { public MyClass() { //construct
I have lots of code like this in my constructors:- function __construct($params) { $this->property
While I'm reading boost/shared_ptr.hpp, i saw this code: // generated copy constructor, destructor are
In this code fragment, which constructor is actually called? Vector v = getVector(); Vector
I wrote this code. The constructor works normally, but in the destructor I get

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.