Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

some confusion about packages

 
sumanth
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic