Question:
how to create packages in java?
2010-03-13 09:10:22 UTC
i created a notepad file pack1 which is a package and i imported it in the other java program but it resulted in errors.the path is same. can anyone help me
Six answers:
Arpita
2010-03-13 23:43:42 UTC
package is a folder...

like we say windows\system32\drivers folder structure as windows.system32.drivers



Normally packages are made to make your java classes unique.. Suppose u and me created same file for eg: BugKiller.java

if both are in default package.. ie no package then it will be difficult for differentiate.. So I will put in a folder i.e. package called arpita and u will put inside package called varsha.. So both are unique.. u need to declare package varsha; in ur java class..

doesn't matter about folders in ur filesystem.. it is the base path.. Suppose if ur file is in C:\java\examples\sources\varsha\Bugkiller.java

If ur base path is C:\java\examples\sources then package of BugKiller.java will be varsha. When u going to run tht class u need to be in base path not inside varsha..

u can create long packages.. for eg.. varsha.game.tictactoe.ui

But u need to create folders according to that..

It gives an hierarchy and better understanding of what that class is intent to..



Suppose u want to use BugKiller in another java file.. If it is in same package then u can use it directly.. If it is in different package(even if that package inside BugKiller's package) then u need to import it inorder to use it.. Otherwise it cudn't find that file..

U can import by

import varsha.BugKiller;

or even important all the classed in one package by

import varsha.*;

Note that it only import classes inside varsha not the classes inside the sub packages..

suppose u want to use classes inside varsha.game package and if u have already imported varsha.* then u need to import for varsha.game package also

import varsha.game.*;
Silent
2010-03-13 09:16:59 UTC
Java packages don't have their own files. Java source files need to have the package name at the beginning like so:



package pack1;



and arranged in folders according to package hierarchy.



Do not follow a package statement with braces. Neeraj Yadav is probably thinking of C# namespaces.
?
2016-05-31 05:38:30 UTC
The problem is that the compiler is looking for the class a1/Game.class Assuming that you have already compiled the Game class. Since the compiler is looking for a1 in the folder State... it is looking for a folder inside this folder that starts with a1. NOTE: Since Yahoo has truncated the text that says what folder you are in, I cannot say what folder you are in and the above information is incorrect. But, if you follow the instructions below, it will work. What you need to do is what another answerer said. Go to the Desktop folder and type in this command. javac a1/Starter.java It should compile then. BUT, it will only compile if both of your classes are in the a1 folder and not in some other folder like a1/stater or a1/game Good luck with it.
instanceof
2014-12-25 07:25:57 UTC
Packages are nothing but all the related classes and interfaces and .class files.

Packages are the folders which are binding all the related ".class" files together.

Binding: making something available to related functionalities.

A simple folder would only groups all the related files . But where as packages are also folders which binds all the related files.

Every package would be a folder. But every folder can not be a package.





We can import packages by using "import".

To import the package

import<.><* or name of class you want to use from that package

To import particular class inside a package>.

import<.>

As per the naming convention: For package names use small case letters.



source:http://instanceofjavaforus.blogspot.in/2014/12/packages-in-java.html
Neeraj Yadav♄
2010-03-13 09:15:22 UTC
package pack1

{

java program

}





while using in other java class

use

import pack1.*;



further visit



http://java.sun.com/docs/books/tutorial/java/package/packages.html

http://www.jarticles.com/package/package_eng.html



hope this helps

Cheers:)
Frecklefoot
2010-03-13 09:13:53 UTC
I don't have enough information to answer this question. But try JavaRanch, tons of experts over there...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...