• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

JAVA Package Query

 
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.....
 
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.



 
Rancher
Posts: 1946
15
Android Java ME Eclipse IDE Java
  • 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.
 
Marshal
Posts: 64680
225
  • 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
Boost this thread!