• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question regarding package and import..

 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two classes , one of them Parent1.java. It is in usr/users/rbg01/java/reddy
and the other child.java is in usr/users/rbg01/java/reddy/test
how should be my package and import names, when i try to compile
child.java it says it can't find the Parent1.java
i have tried giving different paths for import and package statements but it didn't work.,
can some one throw light on this..
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This could be starting with you class path. It needs to have ./ in it to indicate the current directory. Then your source code needs to be placed in the same directory /java.
Remember each directory is actually a different package so anything you wish to share out side the package Constrictors, Methods etc must have public or protected access modifier
After you compile each, copy the class file to the currect subdirectory. Example:
package reddy;
public class Parent1{
public Parent1()
{
System.out.println("Parent Hello");
}
}
package reddy.test;
import reddy.Parent1;
public class child extends Parent1{
public child()
{
System.out.println("child Hello");
}
}

import reddy.*;
import reddy.test.child;
public class run{
public static void main(String args[])
{
child c = new child();
}
}
typeing java run gives the following output
Parent Hello
child Hello
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic