YOUR FEEDBACK
Werner Keil wrote: Java 6 update 10. If I'd be running Apple, I'd probably really drop dead...
AJAXWorld RIA Conference
$300 Savings Expire September 12th. Register Today and SAVE!


2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
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
TOP LINKS YOU MUST CLICK ON


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

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.

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. He is an Adobe Certified Flex Instructor. Currently Yakov works on the book for O'Reilly "Enterprise Application Development with Flex".

YOUR FEEDBACK
RIA News Desk wrote: we would share with you what some of the world's leading rich Internet application pioneers are thinking
Kurt Cagle wrote: There's a growing impedance mismatch between the large scale providers of content and the consumers of that content as we build multiple messaging architectures. How realistically do we resolve this mismatch in such a way that we are able to preserve both flexibility (SOAP), simplicity (Atom) and brevity (JSON), and can we do so without sparking a religious war?
Crolly Darvo wrote: Will the browsers development, unification and standardization give us more possibilities and freedom to sophisticate or simplify our interfaces & APIs?
Brett Green wrote: Do you believe a shift back towards rich desktop apps, which are internet-enabled, will lead away from the need for AJAX-enabled web applications?
Gabriel Kent wrote: If you imagine the a URI is a handle to a given resource -- is the AJAX community pushing to retain the isomorphic relationship between the URI and a given state of a web application as it changes through AJAX interaction?
Micha? S?aby wrote: Are off-line applications for web the right direction? Is Google Gears relevant when more and more devices has 24/7 Internet access? Will web applications of the future be complex on client and lightweight on server side or rather the opposite? This is essential issue to me, as Tigermouse framework I develop favors the later approach.
Marcio wrote: Other questions like: [1] ambiguity in AJAX toolkits, can I match them? how an aspect in Toolkit A can influence toolkit B? The namespaced Web apps becomes now important. It's the same that happened in Browser space, they were different, then become a bit shared, the AJAX toolkits work also may reach a convergence state as we have offline/online caching infra-structure with namespaced events - sandboxed apps in the same page but running each in a given scope. I think the next stage promises good things for us and the current stage is a mess with good value under it. The exploration of the mashup stack and mashup infra for interoperability is an area to massage.
WishList wrote: If only AJAX could somehow bring us a spam-free internet, now THAT would be a rich future!
AJAX vs CF wrote: While Ajax represents the future, it looks like in Georgia they still have developers working in ColdFusion from Adobe - how come? Here's the link: http://www.dot.state.ga.us/
IMHO wrote: Development managers need to ask themselves at least these two questions before adopting AJAX on a project. First, will you make up for the time invested in adopting a new technology through increased development speed? And second, will AJAX allow you to offer a more useful application to your users?
Ahmed ALEM wrote: The answer is definitely: Java + XML + XSLT + a new ML, instead of: JavaScript + XML + HTML. But is there any project which take into account all these ideas? Are there any band of developers who are interested in re-inventing a better wheel?
Answer wrote: The next stage of AJAX is Comet.
mAX kIESELR wrote: It was inevitable that someone would use web 2.0 social aspects together with an AJAX interaction layer to create a next generation weblog. As usual it took a seventeen year old to do it. Logahead is everything I've been looking for recently in blogging software. It's PHP, MySQL, AJAX, and has several social features. DEMO LINK: http://www.maxkiesler.com/index.php/designdemo/fullview/386/
BeyondAJAX wrote: The event-driven web is the most important step for a new Internet in recent years.
LATEST ECLIPSE STORIES . . .
Genuitec announced their expansion into “Strategic Member” status for the Eclipse Foundation. This highest level of Foundation membership ensures both financial and technological backing of the Eclipse platform, as well as increased influence on the platform's evolution through boa...
Furthering its dedication to providing Java developers productivity with choice, Oracle announced the Oracle Enterprise Pack for Eclipse, a new component of Oracle Fusion Middleware. This release marks the first free Eclipse 3.4 environment to support Oracle WebLogic Server 10g Release...
Aptana announced the acquisition of Pydev. The combination of Pydev with Aptana Studio, which is approaching 2.3 million downloads, will bring Aptana's excellence in AJAX development ease to the Python community and bring Python support to Aptana's product lines. The move further reinf...
Two of the biggest launches in Rich Internet Application history took place in 2007/2008 when Adobe launched AIR 1.0 in February '08 and Microsoft launched Silverlight (September '07). At the 6th International AJAXWorld RIA Conference & Expo in October SYS-CON Events is delighted to be...
Red Hat CTO Brian Stevens, Citrix CTO Simon Crosby, Egenera CTO Pete Manca, Allen Stewart, Group Manager, Windows Virtualization at Microsoft, and Brian Duckering, Sr. Director of Products and Alliances at Symantec were the top industry executives who joined Jeremy Geelan in the 4th Fl...
Genuitec announced the availability of MyEclipse Enterprise Workbench 7.0 milestone 1. This milestone release delivers advanced AJAX tooling for Java EE and full Application Lifecycle Management (ALM) capabilities for Eclipse 3.4 Ganymede, among other enhancements.
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