Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing a class from within the package  RSS feed

 
Ranch Hand
Posts: 145
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this code

//PublicBeverageInSamePack.java
package cert;

public class PublicBeverageInSamePack {

void show(){
System.out.println("Beverage in the same pack");
}

}

//TeaInSamePackPublic.java

package cert;

import cert.PublicBeverageInSamePack;

class TeaInSamePackPublic extends PublicBeverageInSamePack {
public static void main(String args[]) {
PublicBeverageInSamePack ob = new PublicBeverageInSamePack();
ob.show();
}

}

When i compiled this program, it gave me the following compile time error.

E:\NancyWork\SunCertification\scjp6\KB\chapter1\cert\TeaInSamePackPublic.java:3: cannot find symbol
symbol : class PublicBeverageInSamePack
location: package cert
import cert.PublicBeverageInSamePack;
^
E:\NancyWork\SunCertification\scjp6\KB\chapter1\cert\TeaInSamePackPublic.java:5: cannot find symbol
symbol: class PublicBeverageInSamePack
class TeaInSamePackPublic extends PublicBeverageInSamePack {
^
E:\NancyWork\SunCertification\scjp6\KB\chapter1\cert\TeaInSamePackPublic.java:7: cannot find symbol
symbol : class PublicBeverageInSamePack
location: class cert.TeaInSamePackPublic
PublicBeverageInSamePack ob = new PublicBeverageInSamePack();
^
E:\NancyWork\SunCertification\scjp6\KB\chapter1\cert\TeaInSamePackPublic.java:7: cannot find symbol
symbol : class PublicBeverageInSamePack
location: class cert.TeaInSamePackPublic
PublicBeverageInSamePack ob = new PublicBeverageInSamePack();
^
4 errors

Process completed with exit code 1

When the same class is shifted to another package it works. Does it mean that class in the same package cannot be imported.
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you compiled it properly?
check whether the PublicBeverageInSamePack.class was in cert package or not?
 
Sheriff
Posts: 9611
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since both TeaInSamePackPublic and PublicBeverageInSamePack classes are in the same package, you don't need to use import...
 
I'm so happy! And I wish to make this tiny ad happy too:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!