Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Junky

Thread: why import a particular class when we are already importing a package.

  1. #1
    Adithi is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default why import a particular class when we are already importing a package.

    In EventHandlig Programs,

    I see programs where both the packages are imported.

    import java.awt.*;
    import java.awt.event.*;

    why do we have to import a particular class when we already import the whole package.

    Can someone please explain.

    Thanks.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: why import a particular class when we are already importing a package.

    Try to compile the following code:
    Java Code:
    import java.util.*;
    import java.sql.*;
    
    class Oops {
        public static void main(String[] args) {
            Date d = new Date();
        }
    }

  3. #3
    Adithi is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default Re: why import a particular class when we are already importing a package.

    I got an error..

    Oops.java:6: error: reference to Date is ambiguous, both class java.sql.Date in
    java.sql and class java.util.Date in java.util match
    Date d = new Date();
    ^
    Oops.java:6: error: reference to Date is ambiguous, both class java.sql.Date in
    java.sql and class java.util.Date in java.util match
    Date d = new Date();
    ^
    2 errors

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: why import a particular class when we are already importing a package.

    Exactly. There is a Date class in both packages. Importing the entire package can lead to errors if both packages contain a class with the same name.

    Another issue is that if I was looking at the code for a class that had 10+ imported packages and then somewhere in my code it used a class that I was uinfamiliar with. How would I know in which package the class was? I know using an IDE it would be simple to navigate to that class but it would be nice if you can glance at the top of the class to see the import statement.

    It really doesn't make a difference to the java bytecode if you import an entire package or just the individual class. I guess it comes down to personal preference and I prefer to implicitly import a single class rather than the entire package.

  5. #5
    Adithi is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default Re: why import a particular class when we are already importing a package.

    So you say that its a good programming to import a particular class which is used in the currrent program to avoid confusion for us with the ambigous errors..

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: why import a particular class when we are already importing a package.

    The main point is that it comes down to personal prefence. If you do have a situation with 2 classes with the same name then you probably have to use fully qualified names anyway.
    Java Code:
    java.util.Date d = new Date();

  7. #7
    Adithi is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default Re: why import a particular class when we are already importing a package.

    Thankyou for the answer.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: why import a particular class when we are already importing a package.

    Also, if you import java.awt.* you are importing all the classes in the directory java/awt, but none of the subdirectories in this folder. Since event is a sub directory in java.awt, you must also import java.awt.event.*, check out the java source to see how the classes are organized. The java source can be found off the Jdk in a did called src.

  9. #9
    Adithi is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default Re: why import a particular class when we are already importing a package.

    Sorry, I dint get you, where to find the java source ie where I can see the organization of classes.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: why import a particular class when we are already importing a package.

    Wherever you put it when you installed java. Should be something similar to
    Java Code:
    C:\program files\java\Jdk\src
    The path will bs similar to this, but not the same.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: why import a particular class when we are already importing a package.

    Or you can look at the API.
    DarrylBurke likes this.

Similar Threads

  1. C : import problem importing HTML file
    By springredfire in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 06-30-2011, 06:42 PM
  2. Importing a zipped package to Eclipse
    By Abder-Rahman in forum Eclipse
    Replies: 10
    Last Post: 02-24-2010, 05:26 PM
  3. importing from default package?
    By diggitydoggz in forum New To Java
    Replies: 6
    Last Post: 01-06-2009, 01:23 AM
  4. Importing / compiling and running with .jar package
    By splinter64uk in forum New To Java
    Replies: 1
    Last Post: 12-05-2007, 02:47 AM
  5. Importing package
    By bugger in forum New To Java
    Replies: 5
    Last Post: 11-26-2007, 12:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •