Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

(un)Ambiguous types (Java OCA 8 Programmer I Study Guide, Sybex)

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm reading the book, on p. 12 Chapter 1 I see the next imports:
import java.util.*;
import java.sql.*; // DOES NOT COMPILE

and the authors tell that there will be error:
The type Date is ambiguous

More often I use IntelliJ so I wanted to test this case.

I write this code:



- and it's OK.

But the code:



gets me an error:

Main.java:7: error: reference to Date is ambiguous
               Date date;
               ^

Am I wrong? If not, please include fix in errata.
 
Andrey Belkin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clarify: JDK 8u92
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a Date object in both java.util and java.sql package. If you just say



with both these packages imported, there will be compilation error. To resolve it you either a) remove one of the imports or b) explicitly state the type like.


 
Andrey Belkin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, yes,  BUT the authors tell about the other mistake:
The type Date is ambiguous
They write exactly this:

but this source DOES compile.
 
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrey Belkin wrote:but this source DOES compile.


No, the source code does not compile

The source code is
The book says

What import could we use if we want the java.util.Date version?


If we try this import
the source code does not compile

However, if we try this import
the source code does compile
 
Ranch Hand
Posts: 182
18
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In page 12 of the book, due to the comment  //DOES NOT COMPILE, it seems to convey LINE2 does not compile. But actually LINE4 doesnt compile with the error msg "The type Date is ambiguous".
 
author & internet detective
Posts: 40912
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is true. We didn't show line 4 in that snippet so we were calling attention to the line causing the issue even though that's not where the compiler give sit.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andrey Belkin,

First of all, a warm welcome to CodeRanch!

Andrey Belkin wrote:I'm reading the book, on p. 12 Chapter 1 I see the next imports:


A few hours before you create a new topic to share your doubts, another rancher did exactly the same about that code snippet

Kind regards,
Roel

PS. You have mentioned you are reading "the book" without specifying which book you are reading. Please note that there are many OCAJP7 and OCAJP8 study guides currently available. So it's always a good practice to mention the resource you have doubts about. I have updated the subject of this topic to add a reference to the study guide. Hope that's ok with you!
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic