Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to call my class in another package?

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have class Pack1 in package s.a,I tried calling it from MainPack.java which extends pack1 as shown below.
Pack1.java is compling but when i try to complie MainPack.java I am getting error.
Can any one tell me if there is anything wrong with my code?
It would be really nice if someone can explain how to complie and run these classes defined in different packages using javac.
MainPack.java is in s folder
-------------------------
package s;
import s.a.*;
public class MainPack extends Pack1 {
public static void main(String args[]) {
Pack1 p = new Pack1();
System.out.println("pack1 i is "+ i);
System.out.println("pack1 k is" + k);
//System.out.println("pack1 j is"+ j);
p.promethod();
ugog();
}
}

Pack1.java is in folder s/a
-------------------------
package s.a;
public class Pack1 {
public int i = 2;
int j = 3;
protected int k = 1;
protected void promethod() {
System.out.println(" inside pack1 promethod");
}
public void ugog() {
System.out.println(" inside u go girl public");
}
}
bani
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are some errors in the MainPack class. See the comments I added below.
package s;
import s.a.*;
public class MainPack extends Pack1 {
public static void main(String args[]) {
Pack1 p = new Pack1();
System.out.println("pack1 i is "+ i); // accessing non-static variable i inside a static context
System.out.println("pack1 k is" + k); // accessing non-static variable k inside a static context
//System.out.println("pack1 j is"+ j);
p.promethod(); // insufficient privilege for accessing a protected member
ugog(); // accessing non-static method ugog() inside a static context
}
}

After you fix the above problems, try compiling in the parent directory of s.
i.e.
javac s\a\MainPack.java
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic