This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

default package

 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
How to import default package and what is classpath for it?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
Firefox Browser Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>java p.b
class a

 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic