• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Package

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to explicatly import it.

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic