aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Package with * Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Package with *" Watch "Package with *" New topic
Author

Package with *

Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
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

Joined: Apr 20, 2009
Posts: 804
    
    1

It depends on your package structure.
* only includes one package (and this is alias for class names), NOT sub packages.


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Anastasia Sirotenko
Ranch Hand

Joined: Jul 20, 2009
Posts: 64
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


[SCJP 6.0]
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

.* 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.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
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

Joined: Aug 16, 2005
Posts: 14269
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
 
subject: Package with *