Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Question about using autoboxing on method overload  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to reproduce the escenario for using autoboxing on method overloading from page 193 (Chapter 4) from the OCA Java SE8 Programmer I Study Guide.

According to the book, the next code:



should compile (since it's not explicitly mentioned it doesn't), and Java should match the int numMiles version when calling it like:



However, I have tested the code and I'm not able to compile it, I am getting the next output from the compiler:

Zoo.java:11: error: reference to fly is ambiguous
fly(3);
^
 both method fly(int) in Zoo and method fly(Integer) in Zoo match
1 error


Did I misunderstand what the book is trying to say? maybe is it some kind of error from the authors not to mention that the code does not compile?

Thanks in advance for the feedback
 
Marshal
Posts: 62846
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Which book is that (authors)? Have you looked in the errata on this link?

Please explain exactly what you are doing; I got both methods to compile on JShell.
 
Ernesto J. Rivera
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ritchie,

Thanks for the welcoming and the reply.

The book Im using to study is:
Boyarski and Selikoff

I have looked on the errata but did not find this in particular, however maybe I'm doing something wrong.

The idea is to have overloaded methods on a class, and in theory, when called the one with the most specific parameter should be used by Java.

So this is the complete class:



This is causing the code not to compile, if I try to compile the class without the fall to fly, it certainly compiles:




However, I don't think this is the idea behind using overloading.
 
author
Posts: 23813
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not allowed to call an instance method, from a static context, without an instance.

... however, once you fix that, the compiler should pick the method that takes an int parameter.

Henry
 
Ernesto J. Rivera
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice catch Henry, you're right, the problem was the access modifier, once corrected, the code compiled. Thanks!
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!