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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:43:38+00:00 2026-05-22T02:43:38+00:00

Could someone help me understand section 15.12.2.5 of the JLS re: most specific method

  • 0

Could someone help me understand section 15.12.2.5 of the JLS re: most specific method?

(bludgeoned cut&paste from JLS follows)

In addition, one variable arity member method named m is more specific than another variable arity member method of the same name if either:

  • One member method has n parameters and the other has k parameters, where n >= k. The types of the parameters of the first member method are T1, . . . , Tn-1 , Tn[], the types of the parameters of the other method are U1, . . . , Uk-1, Uk[]. If the second method is generic then let R1 … Rp p1, be its formal type parameters, let Bl be the declared bound of Rl, 1lp, let A1 … Ap be the actual type arguments inferred (§15.12.2.7) for this invocation under the initial constraints Ti << Ui,1ik-1, Ti << Uk, kin and let Si = Ui[R1 = A1, …, Rp = Ap] 1ik; otherwise let Si = Ui, 1ik. Then:
    for all j from 1 to k-1, Tj <: Sj, and,
    for all j from k to n, Tj <: Sk, and,
    If the second method is a generic method as described above then Al <: Bl[R1 = A1, …, Rp = Ap], 1lp.
  • One member method has k parameters and the other has n parameters, where n >= k. The types of the parameters of the first method are U1, . . . , Uk-1, Uk[], the types of the parameters of the other method are T1, . . ., Tn-1, Tn[]. If the second method is generic then let R1 … Rp p1, be its formal type parameters, let Bl be the declared bound of Rl, 1lp, let A1 … Ap be the actual type arguments inferred (§15.12.2.7) for this invocation under the initial constraints Ui << Ti, 1ik-1, Uk << Ti, kin and let Si = Ti[R1 = A1, …, Rp = Ap] 1in; otherwise let Si = Ti, 1in. Then:
    for all j from 1 to k-1 , Uj <: Sj, and,
    for all j from k to n , Uk <: Sj, and,
    If the second method is a generic method as described above then Al <: Bl[R1 = A1, …, Rp = Ap], 1lp.

Ignoring the issue generics, does this mean varargs is more important than subtyping, or subtyping is more important than varargs, when deciding whether one method is more specific than another? I can’t figure it out.

Concrete example: Which of the following compute() methods is “more specific” according to the JLS?

package com.example.test.reflect;

class JLS15Test
{
    int compute(Object o1, Object o2, Object... others) { return 1; }
    int compute(String s1, Object... others)            { return 2; }

    public static void main(String[] args) 
    {
        JLS15Test y = new JLS15Test();
        System.out.println(y.compute(y,y,y));
        System.out.println(y.compute("hi",y,y));
    }
}

I can’t figure out which is “more specific”; the output prints

1
2

I’m confused how to interpret the results. When the first argument was a String, the compiler picked the method with the more specific subtype. When the first argument was an Object, the compiler picked the method with the fewer number of optional varargs.


NOTE: If you are not reading this section of the JLS, and you are giving an answer that depends on the types of the arguments, you are not helping me. If you carefully read the JLS, other than the parts relating to generics, the definition of “more specific” depends on the declared arguments, not on the actual arguments — this comes into play in other parts of the JLS (can’t find it at the moment).

e.g. for fixed arity methods, compute(String s) would be more specific than compute(Object o). But I’m trying to understand the relevant section of the JLS re: variable arity methods.

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

    After reading the JLS multiple times, I finally think I understand this section.

    What they are saying is that if there are two variable-arity methods, for the purposes of deciding which is “more specific”, you can consider the one with the shorter argument list to be extended so that is of equal length to the longer one. e.g.

    int compute(Object o1, Object o2, Object... others)
    int compute(String s1, Object... others)
    

    can be considered (only for the purposes of “more specific”) to be equivalent to

    int compute(Object o1, Object o2, Object... others)
    int compute(String s1, Object,    Object... others)
    

    and then the argument types are compared, one by one, with the latter method being more specific.

    (more rigorously, the first one has n = 3, k = 2, n >= k, with String <: Object [String is a subtype of Object] and the JLS dictates comparing the types directly of each parameter for j between 1 and k-1 [one less than the shorter length], comparing the vararg type of the shorter method signature with the remaining parameters of the longer method.)

    In the following case:

    int compute(Object o1, Object o2, String... strings)
    int compute(Object o1, String... strings)
    

    these would be equivalent (only for the purposes of “more specific”) to

    int compute(Object o1, Object o2, String... strings)
    int compute(Object o1, String,    String... strings)
    

    and the latter more specific.

    So variable-arity never trumps subtyping for the purposes of comparing “more specific” methods which are both of variable arity.

    However, fixed-arity methods are always considered first (JLS 15.12.2.2 and 15.12.2.3) before variable-arity methods.

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

Sidebar

Related Questions

Could someone help me understand how to preload the images from random.php page so
Could someone help me understand the primitive accessors with this example : i don't
Please could someone help me understand why the div.fl element shown in Developer Tools
I'm new to Flex. Could someone help me understand how Flex generally works with
could someone help me to understand why this errors document.getElementById(actContentToGet).contentWindow.document.body.getElementById is not a function
Could someone help me understand why I'm getting this error: javax.xml.bind.UnmarshalException: unexpected element (uri:,
Could someone help me understand whats going on here, Ive just started using Jquery
Could someone help me understand what is going on in the following Python code
Could someone help me understand why this link works perfectly in firefox but in
I got segmentation fault for the following code, could someone help me understand why?

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.