• 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
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Regarding Package and Import

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have following piece of code:

ClassA in C:\java

ClassB in C:\java

How to import ClassA in ClassB so ClassB compile and execute successfully? Do I need to use classpath to compile and execute ClassB code?
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jnrohit Jain wrote:
How to import ClassA in ClassB so ClassB compile and execute successfully? Do I need to use classpath to compile and execute ClassB code?



Simply. You can't. It is a compile error to import a class from the default package. Only classes in the default package can use classes in the default package. It used to be allowed, but not anymore -- not sure when the rule (specification) was changed though.

Sorry,
Henry
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since ClassB is declared as being in a package com.pkg1 then, since you say it is in C:\java, it is in the wrong directory.
It should be in (assuming C:\java is the root for your source files) in C:\java\com\pkg1.

That's the first thing.
The second is that ClassA should also be in a package.
You'll have issues importing if it isn't.  You can't import from the default (unnamed) package.
 
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remind yourself about packages in the Java™ Tutorials. Also note the section about package names; the package name com.pkg1 shou‍ld belong to whoever owns pkg1.com.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:... the package name com.pkg1 shou‍ld belong to whoever owns pkg1.com.



Someone through GoDaddy, by the look of it.
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but fortunately they didn't seem annoyed, so they probably haven't noticed the package name. Pheww!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic