Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import a class from default package

Possible Duplicate: How to access java-classes in the default-package?


I am using Eclipse 3.5 and I have created a project with some package structure along with the default package. I have one class in default package - Calculations.java and I want to make the use of that class in any of the package (for instance in com.company.calc). When I try to make the use of the class which is in the default package, it's giving me a compiler error. It's not able to recognise the class in default package. Where is the problem?

Calculations.java - source code

public class Calculations {     native public int Calculate(int contextId);     native public double GetProgress(int contextId);     static  {         System.loadLibrary("Calc");     } } 

I can't put my class in any other package. This class has some native methods which are implemented in Delphi. If I put that class in any of the folders, I will have to make changes to that DLL which I want to avoid (really - I can not). That's why I put my class in the default package.

like image 746
Michał Ziober Avatar asked Feb 03 '10 15:02

Michał Ziober


People also ask

How do I import a specific class of a package?

To import the java package into a class, we need to use the java import keyword which is used to access the package and its classes into the java program. Use import to access built-in and user-defined packages into your java source file to refer to a class in another package by directly using its name.

Which class is imported by default in java?

In every java class, java. lang is imported by default.

Which is the default package imported?

lang Package. Java compiler imports java. lang package internally by default.

What is a class by default?

A class with default access has no modifier preceding it in the declaration. Default access is simple in that only the code within the same package can access code with default access. In other words, a class with default access can be seen only by classes within the same package.


2 Answers

From the Java language spec:

It is a compile time error to import a type from the unnamed package.

You'll have to access the class via reflection or some other indirect method.

like image 176
McDowell Avatar answered Sep 23 '22 07:09

McDowell


Classes in the default package cannot be imported by classes in packages. This is why you should not use the default package.

like image 27
matt b Avatar answered Sep 22 '22 07:09

matt b