-
import package problem
Hi there
I had defined an interface 'laugh'in package 'temp' as follows
//the file is c:\work\temp\laugh.java
package temp;
interface laugh{
void eee();
int a=1;
int b=2;
int c=3;
}
and the in temp work directory I created a java code that used the
interface. The code is as follows:
//the file is c:\work\abc.java
import temp.laugh;
class ppp implements laugh{
public void eee()
{
System.out.println("interface function eee() called");
}
}
class abc{
public static void main(String args[])
{
ppp a=new ppp();
a.eee();
}
}
//file ends
My current working directory is c:\work. I have not set CLASSPATH variable;
but that is not the problem since \temp directory is under work ; so it's
accessible from \work which is current working directory.
The problem is that when i give the following command...(from current dir)
javac abc.java
...compile time error comes similar to "temp.laugh is not public in temp".
This is ok. I have not declared 'laught' interface as public so that will
go into default package of java.But when I changed the first statement of
c:\work\abc.java from....
import temp.laugh;
to
import temp.*;
...the program is compiled and run properly. Why didn't java give compile
time error this time? In fact the above change to the import statement
doesn't change the meaning ; so how does it cause 'laugh' to be considered
in spite of NOT being declared as public?
-Dhiraj
-
Re: import package problem
I guess something is wrong with your way of compiling or your classpath.
I copied the following code and I also created the same temp direcotry and
work directoy. I couldn't compile the abc.java in both the cases(with import
temp.laught and import tem.*). The truth is if you won't declare any class
or interface as public that calss/interface can't be accessible from outside
of the package.
"Dhiraj Nilange" <dhiraj_nilange@yahoo.com> wrote:
>
>Hi there
>I had defined an interface 'laugh'in package 'temp' as follows
>//the file is c:\work\temp\laugh.java
>package temp;
>interface laugh{
>void eee();
>int a=1;
>int b=2;
>int c=3;
>}
>
>and the in temp work directory I created a java code that used the
>interface. The code is as follows:
>
>//the file is c:\work\abc.java
>import temp.laugh;
>class ppp implements laugh{
>public void eee()
>{
> System.out.println("interface function eee() called");
>}
>}
>
>class abc{
>public static void main(String args[])
>{
> ppp a=new ppp();
> a.eee();
>}
>}
>//file ends
>
>My current working directory is c:\work. I have not set CLASSPATH variable;
>but that is not the problem since \temp directory is under work ; so it's
>accessible from \work which is current working directory.
>The problem is that when i give the following command...(from current dir)
>
>javac abc.java
>
>...compile time error comes similar to "temp.laugh is not public in temp".
>This is ok. I have not declared 'laught' interface as public so that will
>go into default package of java.But when I changed the first statement of
>c:\work\abc.java from....
>
>import temp.laugh;
> to
>import temp.*;
>
>...the program is compiled and run properly. Why didn't java give compile
>time error this time? In fact the above change to the import statement
>doesn't change the meaning ; so how does it cause 'laugh' to be considered
>in spite of NOT being declared as public?
>-Dhiraj
>
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks