Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package with *

 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • 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.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Anastasia Sirotenko
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • 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
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15435
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic