wood burning stoves 2.0*
The moose likes Mock Exam Errata and the fly likes some confusion about packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "some confusion about packages" Watch "some confusion about packages" New topic
Author

some confusion about packages

sumanth
Greenhorn

Joined: Mar 16, 2001
Posts: 2

File Tree.java

package Flora;
public class Tree {
protected int age;
}

File Forest.java
import Flora.Tree;
public class Forest
{
public static void main (String s[]]
{
Tree t=new Tree();
System.out.println(t.age)
}
}

u compile Tree.java it compile nice.but u compile Forest.java
it gives compile time error.

this two files are same package or not?
plz help me any one.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
sumanth,
Your name doesn't comply with the naming standards here at Javaranch. Check out this link for more info: www.javaranch.com/name.jsp
Please re-register with a more appropriate name.
Thanks,
Bill
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
You classpath has to be setup so it knows where to find that package. Make sure your classpath includes the path to the package Flora.
Bill
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Sumanth,
In order for two files to be considered in the same package, you need to specify the exact same package line. In your Tree.java file you have specified the line:
package Flora;
But in your Forest.java you have not specified any package line. If you don't specify any package line then JVM uses default package. Therefore all files in same directory without package line in them will all be in the same package. To make your stuff work you can do 1 of 2 things.
1. Place line:
package Flora;
inside your file Forest.java. Your import line is then not required.
2. Place Tree.java inside directory named 'Flora', set your classpath correctly, and then it should work.
Regards,
Manfred.
 
 
subject: some confusion about packages
 
Similar Threads
Found a Bug In JDK 1.3.
packages
Cannot find symbol
inheritence
Help with Packages