• 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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: 9613
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...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!