aspose file tools*
The moose likes Beginning Java and the fly likes need help to understand packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need help to understand packages" Watch "need help to understand packages" New topic
Author

need help to understand packages

rajesh baba
Greenhorn

Joined: May 19, 2007
Posts: 25
what a package should contain .java file or a .class file .i hope its a .class file but in jdk 1.4 i see that i had .java files in java package that we use to import various other packages like (example:import java.math.* this folder or package consists of .java files why can any one explain?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Please take the time to choose the correct forum for your posts. The banner on this forum states;
This forum is intended for questions and comments about this web site, www.javaranch.com.
It is not for questions about the Java language - that's what all our other forums are for.


This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

Packages are used to organize your Java programs.

Putting your .java source-code files into a package is not required, but it is highly recommended. Even if you are just learning Java, you should get into the habit of having a package declaration at the beginning of each .java file.



This example is trivial and doesn't really need the package declaration because there isn't much to organize, but it's recommended by Java experts that you get into the habit of using package declarations as early as you can.

Notice that I didn't have an "import" statement. This is because the package: "java.lang" is imported automatically.

In the example above, the class file must be named HelloWorld.java (this filename is case-sensitive).

Because of the package declaration, my .java file will be stored within a hierarchy of directories:

./src/com/kaydell/test/HelloWorld.java

Having a hierarchy here is over-kill, but it is essential in larger programs where you have a .java file for every public class and for every public inerface.

After compiling the .java file, a .class file is created. This can go different places depending upon the options that you specify. It can go into the same directory as the .java source-code, or it might go into another directory such as:

./bin/com/kaydell/test/HelloWorld.class

Hope that helps.

Kaydell
[ May 25, 2007: Message edited by: Kaydell Leavitt ]
Serap Elbeyoglu
Ranch Hand

Joined: Feb 12, 2010
Posts: 52
Hello to all of you,

What is the logic behind of "import" word? If I don't use import javax.swing.JButton, while creating a button, what will happen?


Serap Elbeyoglu
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Try it and see what happens
Serap Elbeyoglu
Ranch Hand

Joined: Feb 12, 2010
Posts: 52
Campbell,

In fact, I want to learn the logic of package names and using import keyword. Who wants to answer?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Campbell Ritchie wrote:Try it and see what happens
There's an answer. What happened when you tried?

You might find something in the Java™ Tutorials, here, or here (try the "using package members" link).
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

What is the logic behind of "import" word?
If I don't use import javax.swing.JButton, while creating a button, what will happen?

import tells the compiler where to look for class files..........................\
so .......javax.swing.JButton......means.......

javax is a name of package and in that package look for swing class and in swing class look for Jbutton

if you remove import javax.swing.JButton,............then there is compiler error..........
because compiler is not able to find where to look for Button specific method..

compiler doesnot understand addButton() etc methods...............

replace javax.swing.JButton with.......javax.swing.*;
will work........becouse this will load the functionality of all my class..............
anything else...........ask me on *****@*****l.com

SCJP6.0,My blog Ranchers from Delhi
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
What did it say in those Java™ Tutorials links I gave you? What did you understand and not understand of it?

Don't ask people to e-mail you; I have deleted your address.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

oposs.............many many apologies...............
now onward i will not type my mail address over there.............

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help to understand packages
 
Similar Threads
Problem in compilation with classpath of jar file
(ANT) Error: No source files and no packages been specified
error while compiling
in the javac compilation output i need *.properties file. how?
Decreasing size of jar file