• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Sybex Practice Tests 11 - Chapter 15 - Q #30

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The answer in the book is "varargs" but my IDE does not want to compile it.
 
Saloon Keeper
Posts: 4346
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "..." that you use are illegal (\u2026). Just use three dots.
 
Maria Mihaila
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:The "..." that you use are illegal (\u2026). Just use three dots.


The " hippo(1, 5); " in the main method does not compile.
 
Piet Souris
Saloon Keeper
Posts: 4346
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you checked your code for other errors? It runs fine with me.

Note that it prints "varargs" (when you get it running), while you would expect it to print: nums. Any idea why that happens?
 
Maria Mihaila
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Have you checked your code for other errors? It runs fine with me.

Note that it prints "varargs" (when you get it running), while you would expect it to print: nums. Any idea why that happens?



It does not print "nums" because it can't be referenced from a static method. But why do I still have the compiling issue? (see the image attached)

Capture.PNG
print-screen of my IDE
print-screen of my IDE
 
Piet Souris
Saloon Keeper
Posts: 4346
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very strange. I have java 10 with NetBeans 11, and it runs fine. Maybe someone else any idea?
 
Ranch Foreman
Posts: 323
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems that the single character '…' is the "Dumb Smart Quotes" of the new decade...

It appeared to me that a Linter was barfing on it rather than the actual compiler?

What are you compiling from?
 
Maria Mihaila
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:It seems that the single character '…' is the "Dumb Smart Quotes" of the new decade...

It appeared to me that a Linter was barfing on it rather than the actual compiler?

What are you compiling from?



I am using IntelliJ but I think I figured out what was wrong.
I've made a very tight connection between the red squiggly underline and compiler errors and I didn't even run the code.
Indeed, it prints "varargs". My bad...

But the question remains: Why isn't a compiler error here if the message is standard compiler error message "java: non-static method x() cannot be referenced from a static context"? Is it because of varargs( hippo(int... nums) ) method?
 
Jesse Silverman
Ranch Foreman
Posts: 323
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but that is legal.  It should realize, as the compiler does, that the varargs method will be called.

We can't really chock it up to varargs being too new, it came out in Java 5 / 2004 I believe.

However, I will say it is really not too common at all (legal, but weird) to have both a static and an instance method with the identical name.

I am guessing it is a minor error that is in the Linter/Checker that IntelliJ uses.
It is normally never noticed because in real life it is very uncommon to have a static and instance method of the same name that vary only by parameter lists.

Just a guess, but if there are no points off for wrong answers, I'm submitting it and moving on  ; )

Just for funsies, if you change the call to the identical-in-meaning:



Does it still bark at you?
 
Maria Mihaila
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:Yes, but that is legal.  It should realize, as the compiler does, that the varargs method will be called.

We can't really chock it up to varargs being too new, it came out in Java 5 / 2004 I believe.

However, I will say it is really not too common at all (legal, but weird) to have both a static and an instance method with the identical name.

I am guessing it is a minor error that is in the Linter/Checker that IntelliJ uses.
It is normally never noticed because in real life it is very uncommon to have a static and instance method of the same name that vary only by parameter lists.

Just a guess, but if there are no points off for wrong answers, I'm submitting it and moving on  ; )

Just for funsies, if you change the call to the identical-in-meaning:



Does it still bark at you?



Yes, I'm having exactly the same error message as before.
 
Jesse Silverman
Ranch Foreman
Posts: 323
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I recall correctly the rules for overload resolution, ignoring the weird case of having a similarly-named static and instance method, within either of those it should be:

1. Exact match found -- you're done, it calls that one.
2. else if there is a simple up-cast to a super-class/wider type, done.  This would not work if you had f(long l, int i) and f(int i, long l) and called with two ints, because it wouldn't be able to pick between two equally valid promotions.
Those two are since the beginning of Java.
3. Straight auto-boxing/un-boxing, it won't both change the type and box/un-box at the same time.  Added with auto-boxing in Java 5 if I remember right.
4. VarArgs is a last resort, it gets called if none of the first 3 get you a resolution when applied.  Also added with Java 5.

Most "best practices" suggestions I've seen say simply "Don't mix varArgs with overloads, it confuses those working with your code" probably because only those writing or taking real exams or mock ones remembers the overload resolution rules in full detail.

I don't remember the rules for if you have an identically named static and instance method.
Unlike Python, where instance methods are basically faked variants, it should be impossible to confuse them with each other.

It was looking to me like the actual compiler finds the varArgs instance method correctly, but whatever logic is doing the squiggles for you in the IDE gets confused here.

It should ideally be impossible to confuse instance and static/class method calls, but since Java allows you to call static/class methods using a reference to an instance, if they exist for the declared, compile-time type of the reference, it can get confusing to humans.  That is why I suggested making the call to be explicitly to the class one.

If I made a mistake here someone else reading this will hopefully disambiguate, otherwise this seems to be a low-priority but real bug for the linter in the IDE you are using.  There is little excuse to do this in production code.
 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic