• 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

Variable Declartion

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I would like to know the pros and cons of defining the variable as follows:
*************
public java.util.ArrayList arrLst;
com.john.ABC abc;
*************
instead of
*************
import java.util.ArrayList;
import com.john.ABC abc;
.....
...
ArrayList arrLst;
ABC abc;
*************
Could ne1 pls let me know
Thanks
John
 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They do exactly the same thing, and have no pros/cons besides that having to type java.util.ArrayList 50 times is kind of a pain
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I first started learning Java I also thought that import statements did some kind of magic. But as Joseph pointed out, import statements are merely a convenience so us programmers don't have to type out long package names repeatedly. They are purely a compile-time convenience. The compiler will use them when it creates the class file (Open a class file in a text editor and you'll see that the complier just prepends the package names on all of your variables for you).
 
John Vergis
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joseph and Blake for the reply.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only time you can't use the import at the top and short name in the code is when you have the same class name from two packages, like when moving data from an XML "Document" to an HTML "Document". I think you have to fully qualify all references then.
I love the help Eclipse gives with this stuff. You can click a fully qualified name and ask it to import it, and organize imports is way cool. Heavily worked code can tend to get a lot of imports that are no longer needed.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"What are the benefits and drawbacks of fully qualified class names versus using Single Type Import Declarations (JLS 7.5.1) ?"
There are no runtime benefits or drawbacks, since the bytecode is precisely the same. There is a significant benefit in using Single Type Import Declarations in the source code, since it improves code readability, and thus, maintenance (my subjective opinion that I believe is shared by many others).
 
John Vergis
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks James and Tony for ur inputs.
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic