This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Package conflicts  RSS feed

 
Sriram Gopal Goli
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The OCAJP study guide says that the following lines of code will throw a compiler error

import java.util.*;
import java.sql.*; //DOES NOT COMPILE

This is because the Date class is present in both util and sql packages and when the class is found in multiple packages, Java gives you the compiler error: The type Date is ambiguous

But I have tried the following code and it does not throw any compiler error and gives the output "Welcome hi"



So is the book wrong?
 
Sriram Gopal Goli
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry. I have never used the Date class. I'm getting the compiler error mentioned in the guide upon using the Date class. Thank You!!!
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book should certainly mention that little fact, that you actually have to use the Date type in your code for the ambiguity to show up, though. Hopefully it does?
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Another such conflict occurs with Timer
 
Andrew Polansky
Ranch Hand
Posts: 310
18
Linux MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imports are just hints for the compiler, which say where it should look for classes.

By providing those two imports to your code, you simply say to your compiler "hey, if you find some class names and you can't find where their classes are located, try to looking in the packages from import statements". Because you didn't use the Date class in your code, the compiler didn't have to look in those packages for this class, therefore no error arises. But when you use Date class in your code, compiler has to look for it in the packages provided in import statements. As it finds two Date classes, the error arises.

More about imports, it's something just for you to write less code. It doesn't affect the program in any way. Every class has its own Fully Qualified Class Name, that consist of its package and class name, ie. java.util.Date, or java.lang.String . Only such names allow classloader to find the right classes. Writing Fully Qualified Class Names everytime would be tiresome and would create a lot of code, decreasing its readability. For this reason there are imports, which allow us to use only class names instead of FQCN. Resolving FQCN is then left to the compiler which looks for the right classes in the imported packages.
 
salvin francis
Bartender
Posts: 1588
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Scheller wrote:Imports are just hints ...

Nice work on mentioning the details, here's a cow!!
 
Andrew Polansky
Ranch Hand
Posts: 310
18
Linux MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Salvin!
 
Sriram Gopal Goli
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your detailed replies.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!