Results 1 to 6 of 6

Thread: import alias

  1. #1
    Pacerier is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default import alias

    Hi all, i was wondering is there anyway to "import alias" in java.

    example i have a package qwe.rty.uio

    how do i import it such that i can refer to qwe.rty.uio with a shorter name like say Z

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Pacerier View Post
    Hi all, i was wondering is there anyway to "import alias" in java.

    example i have a package qwe.rty.uio

    how do i import it such that i can refer to qwe.rty.uio with a shorter name like say Z


    There is no short way of referring to packages, and the need doesn't often arise.

    Classes are another matter. Suppose you have a class qwe.rty.uio.Something, then the import statement itself provides a shorthand way of using the class.

    Java Code:
    import.rty.uio.Something;
    
    Something foo;

    The compiler will recognise Something in the declaration as meaning the class qwe.rty.uio.Something. (None of this is relevant to the runtime.)

    Another thing to note is that the "dot" naming of packages does not represent any special relationship between the packages. The package import.rty.uio.foo and import.rty.uio.bar have nothing to do with one another. So - other than a shorthand to save a few keystrokes of copy-n-paste - it isn't quite clear what Z as a synonym for import.rty.uio would mean in this case.

  3. #3
    Pacerier is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    heys actually i was looking for a way to create "alias" in other words instead of
    new org.apache.commons.fileupload.OmgClass()
    i can type new apac.OmgClass()

    i do not wish to do new OmgClass() in my code because its unclear which package OmgClass belongs to (imagine i have an OmgClass within my current package too)

    i know we can do this in vbnet and c# so i'm basically wondering what's the syntax for doing this in java.. or we cant?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    OK, so you're talking about classes.

    As far as I know you must specify the class to the compiler by either giving its full name, or by having (at most) one import statement. The exact rules (made a little more complex because of the ability to import whatever.*) are in the JLS: Determining the Meaning of a Name. And a description of what packages are is in chapter 7.

    Mostly people import the classes they need using the full name of the class (ie they eschew the whatever.* form of import). And they name things in a way that avoids collisions. (The Java libraries, eg, include a gazillion class and - mostly - avoid conflicts.)

    In C# "using Foo=Foo.Bar.Baz" creates an alias for a namespace. Namespaces are a way of declaring scope that can be nested. As I mentioned Java packages are different and aren't intended to nest in that way (subpackages have little relationship to either parent or siblings.) Packages and namespaces are different things basically.
    Last edited by pbrockway2; 11-23-2010 at 09:02 AM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    As pbrockway2 says, import only what you are actually using (not *) and you will very rarely encounter this problem. The only time I have encountered it is with java.util.Date and java.sql.Date, in which case I import one and fully declare the other. The compiler will inform me when I've gone wrong...:)

  6. #6
    Pacerier is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Help with import
    By eel in forum New To Java
    Replies: 2
    Last Post: 09-19-2010, 10:51 AM
  2. Import
    By gothrog in forum Java Applets
    Replies: 2
    Last Post: 05-13-2010, 11:16 PM
  3. Can not import jxl.*
    By AnnieDing in forum Java Servlet
    Replies: 3
    Last Post: 01-22-2010, 03:52 PM
  4. How could i import DLL in NetBeans?
    By makpandian in forum NetBeans
    Replies: 0
    Last Post: 02-13-2009, 12:53 PM
  5. can any one help?? how to import library
    By libish in forum New To Java
    Replies: 5
    Last Post: 02-12-2009, 06:51 AM

Posting Permissions

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