• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package

 
XinJi Chang
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3340
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to explicatly import it.

 
Jon Poulton
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually the ^ mark should be right after the A like this:

import A;
^
 
Donald Lee VonCannon Jr.
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic