File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes default package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "default package" Watch "default package" New topic
Author

default package

Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Hello
How to import default package and what is classpath for it?


OCA7
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

i think you want to say that how to import classes in default package.

answer is you cannot import classes in default(unnamed package) from some other class. according to JLS

IT is a compiler error if you import classes from default package


only classes in the default package can access other classes in default package.

also never put your classes in default package unless it is for some testing purpose whereby you will use your class and throw it away.


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333




As both classes have no package statements means both are in default package, so Ocjp class could access Oca class without bothering ?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

yes you are right. for classes in same package(in your case since they both are in same but default package) they need not use any import statement or fully qualified class name to access class in the same package. we use import statement to import classes in other package .
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

You're right Mr. Gurpreet, there is a compile error.

I have to write classpath for Oca class

javac -classpath OCA OCJP/Ocjp.java
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

i don't think so there will be compile error. can you please the error you are getting on your end Ankita
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

gurpeet singh wrote:yes you are right. for classes in same package(in your case since they both are in same but default package) they need not use any import statement or fully qualified class name to access class in the same package. we use import statement to import classes in other package .


ankita modi. wrote:You're right Mr. Gurpreet, there is a compile error.


Gurpeet said that classes in the default package can call each other without using import. How is that statement wrong? ... having an incorrect classpath doesn't change that statement.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Henry Wong wrote:
gurpeet singh wrote:yes you are right. for classes in same package(in your case since they both are in same but default package) they need not use any import statement or fully qualified class name to access class in the same package. we use import statement to import classes in other package .


ankita modi. wrote:You're right Mr. Gurpreet, there is a compile error.


Gurpeet said that classes in the default package can call each other without using import. How is that statement wrong? ... having an incorrect classpath doesn't change that statement.

Henry


Yes i meant same,auto import doesn't locate class itself, without classpath there were compile errors.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

first of all there is no such thing as auto-import. your initial question(without your editing) was that can a class in default package use another class in same package without botheration ? now botheration is an abstract thing.
my understanding of "botheration" was writing import statements. so in this context, if classes are in same package( and not just unnamed/default package) they can access each other.

of course if the classes are located at different locations on filesystem then you have to take care of the classpath. classpath concept is different from importing classes.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

gurpeet singh wrote:first of all there is no such thing as auto-import.


Mr. Gurpeet read this carefully,

For convenience, the Java compiler automatically imports three entire packages for each source file: (1) the package with no name, (2) the java.lang package, and (3) the current package (the package for the current file).


SOURCE:javatutorials/java/package/usepkgs.html
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

ankita modi. wrote:
For convenience, the Java compiler automatically imports three entire packages for each source file: (1) the package with no name, (2) the java.lang package, and (3) the current package (the package for the current file).


SOURCE:javatutorials/java/package/usepkgs.html

What tutorial is that? The information is badly dated. Classes in 'the package with no name' cannot be accessed by code in a class that is in a named package.

From The Java Language Specification, Third Edition
Each compilation unit automatically imports all of the public type names declared in the predefined package java.lang, as if the declaration:appeared at the beginning of each compilation unit, immediately following any package statement.


In The Java™ Language Specification, Java SE 7 Edition this information can be found in a different section.
A compilation unit automatically has access to all types declared in its package and also automatically imports all of the public types declared in the predefined package java.lang.


luck, db
There are no new questions, but there may be new answers.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

No,that is correct information.SOURCE:http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html

Although it is true that noname package classes can be access only within same package.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

ankita modi. wrote:No,that is correct information.SOURCE:http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html

It's not correct information, just a tutorial page that hasn't been updated since the rule changed (somewhere around Java 1.2-1.3, I think).

Although it is true that noname package classes can be access only within same package.

Stated differently, classes in a named package cannot access classes in a default package.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

I am not getting difference here.Could you elaborate it ?

From The Java Language Specification, Third Edition
Each compilation unit automatically imports all of the public type names declared in the predefined package java.lang, as if the declaration:appeared at the beginning of each compilation unit, immediately following any package statement.


In The Java™ Language Specification, Java SE 7 Edition this information can be found in a different section.
A compilation unit automatically has access to all types declared in its package and also automatically imports all of the public types declared in the predefined package java.lang.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39


ankita modi. wrote:I am not getting difference here.Could you elaborate it ?


Basically...

ankita modi. wrote:
For convenience, the Java compiler automatically imports three entire packages for each source file: (1) the package with no name, (2) the java.lang package, and (3) the current package (the package for the current file).


SOURCE:javatutorials/java/package/usepkgs.html


The source is outdated.... as point (1) is wrong. From your specification quotes....

ankita modi. wrote:
From The Java Language Specification, Third Edition
Each compilation unit automatically imports all of the public type names declared in the predefined package java.lang, as if the declaration:appeared at the beginning of each compilation unit, immediately following any package statement.


In The Java™ Language Specification, Java SE 7 Edition this information can be found in a different section.
A compilation unit automatically has access to all types declared in its package and also automatically imports all of the public types declared in the predefined package java.lang.


Notice that there is no mention of point (1).

Henry
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 865
    
    5
>java p.b
class a

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Ivan Jozsef Balazs wrote:>java p.b
class a



Yes. It is possible for a class in a package to use a class in the default package via the reflection library.... but ... Do you really want to use a class this way? Care to show us how you would access a field or call a method -- as it is pretty annoying to do with reflection.

Henry
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 865
    
    5
Henry Wong wrote:Care to show us how you would access a field or call a method -- as it is pretty annoying to do with reflection.


That exercise is left to the worthy reader.

I did not say (let alone suggest) you should use it, I just demonstrated the feasibility of this trick.
 
Consider Paul's rocket mass heater.
 
subject: default package
 
Similar Threads
constructors access
Abstract class
package problems
Basic Jsp
access modifiers and inheritance