• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Package Declaration

 
Greenhorn
Posts: 23
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I'm bit confuse with package declaration... As per what I have got is, suppose I am declaring a package in my source code as

package myPackage...

class myClass {
varaiables...
methods...

}

I've to save this source file in a folder with the same name as package.. e.g. c:\users\me\desktop\java\myPackage

I got up to this,

but the following code I found it in OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808 Chapter 1\review questions\Q-7



I didn't get package creation as package aquarium.jellies... I don't know how this create subdirectory in package statement as aquarium.jellies ??? There is nothing stats in the book's chapter even not in java tutorial I found something like package com.oracle.java.oca ... but not the local directory like the one I wanted to know.

Can someone explain it to me how this works and if possible please show am an example.

Thanks,
S B Patel
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The source of public aquarium.Water class should be in a file ./aquarium/Water.java.

The source of public aquarium.jellies.Water class should be in a file ./aquarium/jellies/Water.java.

If you want to use one of Water classes you need to import one.
import aquarium.Water;
or
import aquarium.jellies.Water;

If you don't import a class you can still use it but it would require using its fully qualified name:
 
Marshal
Posts: 76867
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S B Patel wrote:. . . I've to save this source file in a folder with the same name as package.. e.g. c:\users\me\desktop\java\myPackage . . .

It is more complicated than that, I am afraid.
It is a good idea to have source and .class files in the same folder, but the package mechanism allows you to have them in different folders. You can have the .java files in c:\users\me\desktop\java\src\myPackage and the .class files in c:\users\me\desktop\java\classes\myPackage.
 
Suketu Patel
Greenhorn
Posts: 23
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:
The source of public aquarium.jellies.Water class should be in a file ./aquarium/jellies/Water.java.



I didn't get it yet...

---One Water class is saved in package aquarium
We have to save this .java file somewhere .\aquarium

---Second Water class is saved in package aquarium\jellies
We save this .java file in .\aquarium\jellies

What is my confusion is whey we declare second Water class as package aquarium\jellies... as we have to save it manually by creating subfolder under aquarium folder. Why we don't do it as package jellies and save it as .\aquarium\jellies\jellies.java

OR

Please correct me if I am wrong... We are doing it this way because we need to show a full path.

Thank you all for the reply,
S B Patel.
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

We are doing it this way because we need to show a full path.


Right. Even though it looks like the package statement is a hierarchy, this is mostly for the humans that read it. package aquarium and package aquarium.jellies are two completely different packages as far as the package statement is concerned, just as package jellies would be completely different from package aquarium.jellies.
 
Suketu Patel
Greenhorn
Posts: 23
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

First of all thank you very much for your given time and your reply...

I have cleared my doubt about why declaring package as

package aquarium and
package aquarium.jellies

What I found is when you declare package aquarium.jellies for Water class... you can compile it from somewhere like javac abc\xyz\aquarium\jellies\Water.java and it will create a Water.class file under aquarioum.jellies package. BUT you can't run Water.class file from anywhere else than aquarium\jellies directory e.g. java aquarium.jellies.Water

If I am not wrong that is why declaring a package like that...

Thanks,
S B Patel
 
You have to be odd to be #1 - Seuss. An odd little ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic