aspose file tools*
The moose likes Beginning Java and the fly likes Package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package" Watch "Package" New topic
Author

Package

XinJi Chang
Greenhorn

Joined: Feb 18, 2004
Posts: 4
Hi,
I have two files, A.java and B.java,
and I put A.java in C:\, B.java in C:\Test\

A.java doesen't set package:


B.jave sets package Test


Can I creat A type object in B class?
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
You need to explicatly import it.



I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Jon Poulton
Greenhorn

Joined: Jun 09, 2004
Posts: 27
XinJi,
If you dont specify a package for a class, then the class automatically gets put into a package called the "Default package", which is effectively an 'invisible' package.
Just like other packages, the classes in the default package can access each others public methods and constructors. However, if you then have other classes that try to use classes in the default package which are not themselves in the default package, then you have to import them as shown in the post above.
Jon
Jon Poulton
Greenhorn

Joined: Jun 09, 2004
Posts: 27
Sorry, the first sentance in the second paragraph is very clear. What I mean to say is that classes in the same package (including all those in the default package) do not have to use import declarations to access each other. As soon as you start using classes in other packages, you have to use import those classes at the top of the file before you can use them.
Jon
Donald Lee VonCannon Jr.
Greenhorn

Joined: Sep 15, 2004
Posts: 3
Has anyone tried compiling this. I created those exact files and tried compiling and this is what it gave me:

C:\>javac .\Test\B.java
.\Test\B.java:2: '.' expected
import A;
^
1 error

I am replying because I am having this same problem in a much bigger application and I need help. So please if you have any input I thank you.
Donald Lee VonCannon Jr.
Greenhorn

Joined: Sep 15, 2004
Posts: 3
actually the ^ mark should be right after the A like this:

import A;
^
Donald Lee VonCannon Jr.
Greenhorn

Joined: Sep 15, 2004
Posts: 3
Ok, I can't get it to position it below the ; but that is where it is supposed to be. Try writing those file and compiling them yourselves. You should get the same error.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Use of the default package is very strongly discouraged.

It used to be the case that you could import a class from the default package into a named package as shown, but it was never explicitly stated in the Java Language Spec that this was legal. Recent versions of the spec state that it is illegal, and recent versions of javac now reject this syntax. As a result, doing what you need to do is now not possible.

Solution: don't use the default package.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Package