• 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 with *

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we use
package org.ex.*;

So far as i have read i know we cant use *(asterik sign) with package, but nowhere it is written like that, So please confirm me.
I know its simple but i want to be 100% sure on this.
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on your package structure.
* only includes one package (and this is alias for class names), NOT sub packages.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajesh k Jha wrote:Can we use
package org.ex.*;

So far as i have read i know we cant use *(asterik sign) with package, but nowhere it is written like that, So please confirm me.
I know its simple but i want to be 100% sure on this.



you can not use

but you can use

The difference is: with package you define the package that will contain your class, and a class can be a member of just one package.

But with import you define which classes will be used in your class code, so it can be any amount of classes, and you should define the classes with their package names.
It can be
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.* is used to specify that all classes should included in the package. But when you use package statement in a source file we will be putting the class defined in the source file in the the specified package. package org.*; is not a valid.
 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now i can conclude that we should try to avoid the use of * with packages(Even if one class is there in the package) but can use * with import statement freely.

Thanks to all
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using * in a package statement doesn't work.

You can use * in import statements, but often it is better to import classes explicitly one by one, instead of importing everything in a package with *. That's because using import with * can make your program not compile anymore with a future version of Java (or a future version of a third-party library you're importing).

If, in a future version, a class is added to a package that you're importing with "import somepackage.*;" and that new class happens to have the same name as another class that you're using in the same source file, then the compiler suddenly has two classes that have the same (non-qualified) name, and your program won't compile anymore.

When you import only the classes that you really need from 'somepackage', you won't automatically import new classes from 'somepackage' when a new version of that package is used and you won't have a name clash problem.
 
Bras cause cancer. And tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic