• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

packages:::::::

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assalam-o-Alakum:-
I am learning a new topic called packages. interesting one but there is a problem, I am unable to understand untill now how we compile a package which have two or more classes and how to run a package.
So, if some one answer me I am very thankful to him. I know packages but the problem is for command to compile and run the packages.

Thanks.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adnan
You cannot compile or run a package in java.
Package is something used to avoid name collisions. Its like a namespace and has an hierarchy.
for example....
in my .jar file there are two classes
Person.class - under com/prasad
Person.class - under com/prasad/Misc
if this jar file is in the classpath and I try to compile the following code I get compiler errors
Person = new Person();
because the compiler doesnot know which Person to refer.
to make it clear to the compiler I refer them I can refer like this....
com.prasad.Person n = new com.prasad.Person();
or
com.prasad.Misc.Person n = new com.prasad.Misc.Person();
another way to do this is.. importing the specific package
forexample
import com.prasad.*;
Person = new Person();
or
import com.prasad.Misc.*;
Person = new Person();
but you cannot import both packages when you create a new Person Object
import com.prasad.*;
import com.prasad.Misc.*;
Person = new Person();
The compiler complains about the ambiguity.
Now how to create a package?
Just write this statemet at the begining of your .java file
package <packagename>
example:
package com.prasad;
public class Person
{
....
}
package com.prasad.Misc;
public class Person extends xxx
{
.....
}
Hope this helps
cheers
Siva
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Adnan!
for compiling yr. java files with package notation intact use the following.
c:>javac -d AnyJavafile.java
then it creates a directory structure with yr.package name as parent to the class file.
for execution of such class files
use the following
java package.anyclassfile
Nageswara
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, Java life is easier if you remember this rule: ALL class names must ALWAYS be qualified with their package. (By default java imports "java.lang,*", which make this rule less obvious.) For example, to reference class A in package mypackage you must code "mypackage.A" or use an import statement. If "A" has a main method then you run it by passing "mypackage.A" to java. This assumes your classpath is set. I.e., that your classpath includes the root location where directory "mypackage" is found.
 
It's never done THAT before. Explain it to me tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic