• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Package conflicts

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Another such conflict occurs with Timer
 
Ranch Hand
Posts: 310
18
MS IE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MS IE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Salvin!
 
Sriram Gopal Goli
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your detailed replies.
 
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic