Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAVA Package Query

 
Tulika Rai
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created 2 packages pkg1 and pkg2, my directory structure is :

c:\ABC\pkg1
c:\ABC\pkg2

I have created a file TestProtectedSuper.java in pkg1 and TestProtectedSub.java in pkg2

when I am individually compiling and running pkg1.TestProtectedSuper it is running fine, but when I am trying to use this file in pkg2.TestProtectedSub.java,
getting compilation error :

package pkg1 does not exist

cannot fine symbol for class TestProtectedSuper

code for TestProtectedSuper.java :

package pkg1;

public class TestProtectedSuper
{
protected int age;
public TestProtectedSuper(int ag)
{
age=ag;
}

/*public static void main(String args[])
{
System.out.println("TestProtectedSuper");
}*/

}

Code for TestProtectedSub.java is :

package pkg2;
import pkg1.TestProtectedSuper;
public class TestProtectedSub
{
public static void main(String[] args)
{
System.out.println("Testing Protected Variable in different package");

TestProtectedSuper obj1=new TestProtectedSuper(10);
System.out.println("age is : " + obj1.age);
}
}

Can anyone help me in solving my problem.....
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because age variable has protected access modifier and hence can not be accessed by any class but subclass outside the package.



 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

because age variable has protected access modifier and hence can not be accessed by any class but subclass outside the package.


The above should have raised an error in this line, and which is obviously going to occur



But the error which occurred first is


package pkg1 does not exist


So it looks like a classpath issue.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48957
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might simply be an error about which directory the command line uses as current. Try running it from the directory one level above pkg1, ie ABC.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic