YOUR FEEDBACK
More on the Software Assembly Question - Do Design Patterns Help?
Yanic wrote: Hi, > UML and MDA are being changed to be more data and doc...
SOA World Conference
Virtualization Conference
$50 Savings Expire May 23, 2008... – Register Today!


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TODAY'S TOP SOA & WEBSERVICES LINKS


Java Basics: Lesson 11, Java Packages and Imports (Live Video Education)
Lesson 11 in the Hugely Popular "Java Basics" Series by JDJ Editorial Board Member Yakov Fain

Digg This!

Page 1 of 3   next page »

Java comes with thousands of classes that are organized in packages (similar to files and directories on you disk). Some packages include classes responsible for drawing, while other have classes for the Internet access, and so on. For example the class String is located in the package called java.lang, and the fully qualified name of this class is java.lang.String.

The Java compiler only knows where to find classes that are located in the package java.lang, but there are many other packages with useful classes, and it's your responsibility to let the compiler know where the classes that are used in your program live. For example, the package java.io contains classes responsible for input/output operations, while most of the Swing classes live in the following two packages:


javax.swing
javax.swing.event

It would be annoying to write a full class name every time you use it, for example:

javax.swing.JButton myButton = new javax.swing.JButton();
javax.swing.JFrame myFrame = new javax.swing.JFrame();

To avoid this you can use import statements right above the class declaration line, for example:

import javax.swing.JFrame;
import javax.swing.JButton;

class Calculator{
JButton myButton = new JButton();
JFrame myFrame = new JFrame();
}

These import statements allow you to use short class names like JFrame or JButton, and the Java compiler will know where to find them. Please note, that nothing is actually imported into your program: it's just a name resolution mechanism that helps the compiler to find classes and make your program more readable. If your need to use several classes from the same package, you do not have to list each of them in the import statement, just use the wild card. In the following example the asterisk (*) makes all classes from the package javax.swing visible to your program:

import javax.swing.*;

Still, it's better to use separate import statements, so you can see clearly which classes are imported from a particular package.

When programmers work on large projects that have lots of classes, they usually organize them in different packages. For example, one package can have all classes that display graphical windows, while another can contain data access classes.

Let's create a new project called PingPong in the Eclipse IDE. This project will have classes in two packages: screens and engine. Now create a new class PingPongTable and enter the word screens in the field Package:  

Press the button Finish and Eclipse will generate the code that will include the line with the package name.

package screens;

public class PingPongTable {

public static void main(String[] args) {
}
}

By the way, if your class includes the line with the keyword package, you are not allowed to write anything but the program comments above this line.

Since each package is stored in a different folder on a disk, Eclipse creates the folder called screens and puts the file PinPongTable.java there. Check it out - there should be a folder c:\eclipse\workspace\PingPong\screens on your disk with files PingPongTable.java and PingPongTable.class.

Now create another class called PingPongEngine and enter the word engine as the package name. The PingPong project has two packages now:

Since our two classes are located in two different packages (and folders), the class PingPongTable won't see the class PingPongEngine unless you add the appropriate import statement.

package screens;

import engine.PingPongEngine;

public class PingPongTable {

public static void main(String[] args) {
  PingPongEngine gameEngine = new
      PingPongEngine();
}
}

Java packages not only help better organize your classes, but also can be used to hide their classes from the "foreigners" living in other packages. In Java you can use public, private and protected keywords to specify the access level to a particular method or a class. But if you do not use any of these keywords in the method or class declaration, you'll be able to access them only from the classes located in the same package. We've discussed access levels briefly in Lesson 3 of this series.


Page 1 of 3   next page »

About Yakov Fain
Yakov Fain is a managing principal of Farata Systems, consulting, training and product company. He has authored several Java books, dozens of technical articles. SYS-CON Books released his latest co-authored book , "Rich Internet Applications with Adobe Flex and Java: Secrets of the Masters" in Spring 2007. Sun Microsystems has nominated and awarded Yakov with the title Java Champion. He leads the Princeton Java Users Group. Yakov teaches Java and Flex 2 part time at New York University. He is an Adobe Certified Flex Instructor and an Editor-in-Chief of Flex Developers Journal.

Howard N-H wrote: I'm glad you mentioned the use of interfaces as repositories of constants. While it may not be recommended in the strict sense of why interfaces exist, I've used this technique for years on large projects as a good way of organizing and reusing constants.
read & respond »
static imports wrote: So you can use wildcards in the import? Can you use static import for static classes?
read & respond »
Java Basics series wrote: Will the other 10 lessons be on SYS-CON.TV?
read & respond »
Java Live wrote: Is it Yakov Fain who is speaking at the moment? A live Java lesson - cool!
read & respond »
sundeep wrote: I used this lesson in conjunction with the last one, lesson ten on eclipse IDE and it really helped me
read & respond »
XML JOURNAL LATEST STORIES . . .
3rd International Virtualization Conference & Expo: Themes & Topics
From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discussed in NYC June 23-24, 2008 by the world-class speaker faculty at the 3rd International Virtualization Conference & Expo being held by SYS-CON Events in The Roosevelt Hotel, in midtown
EDI to XML: A Practical Approach
While EDI transactions account for most worldwide commercial activity, XML-based alternatives are beginning to gain traction. According to Forrester Research, stateful XML, stateless XML, and even flat file exchanges are all projected to grow at a faster rate than EDI over the next few
Red Hat Named "Platinum Sponsor" of Virtualization Conference & Expo
Red Hat is a trusted open source provider. Red Hat offers enterprise customers a long-term plan for building infrastructures on the quality and innovation of open source. Combining open source operating system platform, Red Hat Enterprise Linux, together with applications, management
JustSystems Contributes Key XBRL Rendering Technology to Financial Community
JustSystems announced that it is contributing intellectual property rights for its invention of eXtensible Business Reporting Language (XBRL) rendering technologies to XBRL International, the standards body responsible for the oversight of the XBRL specification. The invention, known a
JustSystems Launches Campaign for XBRL Success
JustSystems announced its campaign to help organizations adopt XBRL (eXtensible Business Reporting Language), the XML-based standard for communicating financial and business information. In related news, JustSystems also announced that it has contributed intellectual property rights of
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS


ADS BY GOOGLE
BREAKING XML NEWS
Woodstream Selects EXTOL Business Integrator to Improve Business Processes, Customer Collaboration and Internal Integration
Woodstream, providers of pet, lawn-care and animal-friendly brands such as Perky-Pet,