• Post Reply Bookmark Topic Watch Topic
  • New Topic

package  RSS feed

 
sparsh khandelwal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone,
consider two conditions:
1) we give the statement in java file
2) we save the java file in a window folder named as mypackage.
in short difference between a package and a window directory ?
please help
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
personally, I save mine in Unix directories, not windows.
 
sparsh khandelwal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply,
even if you save your file in unix directory then what is the difference between package and directory?

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a package in java is a namespace. it "assembles" related class files under a common "tree". a directory is file which holds other folders/files. the directory structure where you save your .java files should match package structure.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A package is a name space to which a class belongs. It determines the fully qualified name of a class, and which classes can interact with each other and how.

A directory is a structure on the file system that helps organize files.

The javac compiler and the JVM by default look for source and class files in a directory path that reflects the package name. This is not required though. Package names are an integral part of Java code. Directories can be used to store files in, but don't necessarily have anything to do with Java.
 
sparsh khandelwal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply
i agree with the answer given above
but what i have read that we use packages so that user need not to concern about the file name , same we can do by creating the directory manually.
any advantage of using package over directory
help
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two are not related, they serve a different purpose.

Let's assume we have two independent developers, who happen to create a class with the same name. Let's call it MainClass. What if a client used software by both these developers? How would the client's code know which of the two classes to use if you referred to MainClass? Package names provide developers a way to assign classes to a name space, so that different classes can be more easily distinguished: dev.one.MainClass is different from dev.two.MainClass.

Now, when you tell the JVM to run dev.one.MainClass, how would the class loader know where to find classes it refers to? Using a directory structure that's similar to the package name makes it very easy to locate these files.

So why don't we get rid of the package statements and just determine which class we are using from their directory structure alone? Because some classes aren't located in directories. Classes can be loaded from a network, or can even be created and loaded at runtime, without ever existing on a file system.

Hopefully you now see why we need both directory structures and package statements, because both fulfill a different role.
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two sectrions in the Java Tutorials about this, which I think you should read: 1 2.
 
sparsh khandelwal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the meaning of name space here ?
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Google namespace.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!