• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Andrey Belkin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clarify: JDK 8u92
 
K. Tsang
Bartender
Posts: 3583
16
Android Java
  • Mark post as helpful
  • send pies
  • 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: 8
  • Mark post as helpful
  • send pies
  • 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.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • 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
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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".
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic