® it takes two to tango – java and eclipse john kidd – kiddcorp, l.p. paul t. calhoun –...

152

Upload: phoebe-sharp

Post on 11-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207
Page 2: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

®

It Takes Two to Tango – Java and Eclipse

John Kidd – Kiddcorp, L.P.

Paul T. Calhoun – NetNotes Solutions Unlimited, Inc

JMP207

Page 3: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Your Presenters – This Is Us!

John KiddPresidentKiddcorp, L.P.

John Kidd is a recognized strategist, as well as a frequent speaker and author in the area of Java and J2EE technologies. John has more than 16 years experience in leading teams to deliver line of business applications for companies such as Belo, IBM/Lotus, Texas Instruments, and Advanced PCS.

John has been an early adopter in delivering key technologies and delivery methods. He is a frequent speaker on IT strategies and a consultant for organizations developing open source strategies.

Page 4: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Your Presenters – This Is Us!

Paul T. CalhounChief Technology OfficerNetNotes Solutions Unlimited

Paul Calhoun, ND 6,7 and 8 PCLI and PCLP,is a highly rated speaker who provides customer-focused knowledge transfer and consulting to Fortune 100 and 500 companies, as well as many SMBs. Paul currently builds Domino, Web, Java, and XML applications for his customers using Domino, Portlet Factory and WebSphere.

He co-authored the IBM Redbook “XML Powered by Domino,” and has developed several online XML and Java programming courses. He has written articles for both “The View” and “The Sphere” technical publications.

Page 5: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

The Great Book Give Away

Compliments of O’Reilly Publishing Please thank them by buying their books !!!!

If you want to participate in the book drawings Place your Business card (or first and last name) in the container being

passed around We WILL NOT harvest contact information for ANY marketing

purposes

We will draw the names during the Session

Page 6: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Agenda

Roadmap

Eclipse, The Tool

Java language Fundamentals

Developing Java code In Eclipse

Develop and Deploy Lotus Domino Java code using the Eclipse development platform

Wrap Up

Page 7: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Roadmap

I’ve said it before, so I’ll say it again !!

Learning Java™ is a lot like eating an Elephant !!

It’s a big job and there’s no clear place where to start !!

Page 8: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Roadmap

Start Here! J2SE

Servlets, JSPs & JSFs

J2EE, XML

Page 9: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Roadmap

Start with J2SE (Java 2 STANDARD Edition) This covers core Java functionality

Syntax Data Types Constructs Core Classes

– java.lang

– java.io

– java.net

– etc. Allow 3-6 Months

http://java.sun.com/javase/index.jsp

Page 10: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Roadmap

Learn XML (not part of Sun Certification) XML Syntax DTD/XML Schema XSL

XSLT XSL:FO

Finally jump on the J2EE bandwagon (In this order) Servlets JSPs JSFs Allow another 3-6 months

Then the rest of the J2EE specification Allow another 3-6 months

Page 11: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Roadmap

Start small Java Agents in Domino

Graduate to Servlets Agent Code transitioned to Servlets running on Domino

Incorporate an external Servlet/JSP engine Apache w/Tomcat add-in (DSAPI Filters available) JBOSS (DSAPI Filters available) WebSphere Application Server (DSAPI Filters available)

Write JSP/JSF applications that access Domino data Using Domino Custom JSP tags (Available since ND 6)

Page 12: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Roadblocks

Road Blocks on your Journey to Learning Java “Linear” thinking instead of thinking in “Objects” Starting to learn Java with J2EE applications (Servlets, JSPs) Trying to start with the Java Enterprise Technologies Try to learn Java in conjunction with a HUGE Mission critical project Not applying what you learn EVERYDAY !!

Page 13: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Agenda

Roadmap

Eclipse, The Tool

Java language Fundamentals

Developing Java code In Eclipse

Develop and Deploy Lotus Domino Java code using the Eclipse development platform

Wrap Up

Page 14: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Eclipse, The Tool

Installing Eclipse

Updating Eclipse

Configuring Eclipse

Navigating Eclipse

Creating Projects

Page 15: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Ok, let’s get this out of the way

We Don’t Hate LotusScript !!!!!!!!!!!

We Don’t Hate LotusScript !!!!!!!!!!!

We Don’t Hate LotusScript !!!!!!!!!!!

We Don’t Hate LotusScript !!!!!!!!!!!

We Don’t Hate LotusScript !!!!!!!!!!!

We Don’t Hate LotusScript !!!!!!!!!!!

We Don’t Hate LotusScript !!!!!!!!!!!

We Don’t Hate LotusScript !!!!!!!!!!!

Page 16: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Very Wise Saying

If the only tool you have is a hammer, you tend to see every problem as a nail

Abraham Maslow

Page 17: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Your Domino Development Toolbox

Formula Language

LotusScript

Java JavaScript

Page 18: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Installing Eclipse

Steps to Install Eclipse Download a Java SDK FIRST

Level 1.4.2 or higher

– Java 5 is recommended Install Java SDK

Download Eclipse 3.3.x (Europa Build) http://www.eclipse.org/downloads/ Supported Platforms

– Windows

– Linux

– Solaris 8

– AIX

– HP-UX

– Max OS X

Page 19: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Installing Eclipse

You must have at least one Java SDK installed on your machine BEFORE installing the Eclipse workbench Must be version 1.4.2 or above (J2SE 5 or 6)

Java 5 is the recommended !!

If supporting multiple installed SDKs Install oldest to newest

Installing Java 5 or 6 will allow you to write code that conforms to older JRE

http://java.sun.com/javase/downloads/index.jsp

Tip

Page 20: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Installing Eclipse

Download Eclipse 3.3.x This is the Europa build It will be .zip file Unzip the contents to a directory on the hard drive

That’s it. You just installed Eclipse !!!

Start Eclipse Eclipse.exe (Windows) ./Eclipse (Linux) First time Eclipse starts it will search for an installed SDK and add it to the

workbench configuration Only the currently configured JRE will be added to the workbench

configuration Others can be added (See configuration slides)

Page 21: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Installing Eclipse

Eclipse start up screen

Page 22: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Installing Eclipse

The Eclipse Workbench with the default Java Perspective

Page 23: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Updating Eclipse

The first task to complete after the Eclipse workbench is started is to check for software updates From the “Help” menu choose “Software Updates > Find and Install…”

Page 24: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Updating Eclipse

In the Feature Updates Dialog box there are two options Search for updates of the currently installed features

Allows you to update all currently installed feature functionality Search for new features to install

Allows you to add additional functionality to the Eclipse Workbench

– Like the Web Tools Plug-in

Page 25: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Updating Eclipse

Search for updates to the currently installed features FIRST This will ensure that you have the most up to date “Core” install

If you have just downloaded Eclipse, there may be not updates to install

After the installed feature update is complete, then search for new features to install This will allow you to add additional functionality to your installation of

Eclipse

Page 26: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Updating Eclipse

If “Search for new features to install” is selected you will see the following dialog box

Page 27: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Updating Eclipse

The Europa Discovery Site is a combination of Eclipse projects in one installation site Check the values for the functionality you want to install and click the

Next button If there are errors after you make your selections, click the “Select

Required” button to resolve any dependency issues

Page 28: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Updating Eclipse

When first starting out I recommend including the following Graphical Editors and Frameworks Java Development Web and JEE Development

You will need to run Software update After every newly installed feature At least once a Quarter Preferably once a month

Tip

Page 29: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Configuring Eclipse

Configuration changes that affect the entire workbench are done via the “Preferences” dialog box From the “Window” menu choose “Preferences”

Tip

Page 30: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Configuring Eclipse

Expand the preferences in the column on the left to access the preferences for that category

Page 31: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Configuring Eclipse

Common preference changes Change Font for editors and dialogs

General > Appearance > Colors and Fonts

– Expand “Basic” select “Text Font” Prompt for workspace on startup

General > Startup and Shutdown Default Web Browser to use

General > Web Browser Enable HTTP Proxy Connection

General > Network Connections

Page 32: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Configuring Eclipse

Common preference changes (Cont) Configure additional JREs

Java > Installed JREs

– Click the Add button and navigate to install directory

– You can add the Domino 6.x, 7.x and 8.x JVM to the Installed JREs in Eclipse to use on Domino only projects

Page 33: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Configuring Eclipse

Common preference changes (Cont) Some changes are specific to the current workspace (Like the JVM’s we

just added) Preferences can be exported from one workspace and then imported

into another Once you have configured your preferences the way you want them

Export the Preferences

– File > Export…

– Expand General and choose “Preferences”

Tip

Page 34: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Configuring Eclipse

Common preference changes (Cont) You can choose to export all or just the specific changes Provide a file name that has an .epf extension

This is just an XML file

Page 35: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Configuring Eclipse

Common preference changes (Cont) Preferences can be imported into a new workspace from the import

menu File > Import…

– Expand General and choose “Preferences”

Page 36: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Configuring Eclipse

Common preference changes (Cont) Choose the .epf file to be imported

You can import all of the preferences or choose a sub-set

Page 37: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Configuring Eclipse

Common preference changes (Cont) In order to test Web Applications (Servlets, JSP’s) you will need an

installed Web Application Server Add Runtime Test Server

– Server > Installed Runtimes

– Click “Add” and browse to installation of runtime test server

– IBM WebSphere Application Server

– Apache w/Tomcat add-in

– JBOSS

– Oracle

Page 38: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Navigating Eclipse

Eclipse is made up of Perspectives

Allows developer to focus on particular development type

– Java

– J2EE

– Debug

– Plug-in

– Resource Views

Displays functionality with-in a perspective

– Package Explorer

– Problems Editors

Allows for development of a particular code type

– Java

– HTML, CSS, JavaScript, XML, etc.

Page 39: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Navigating Eclipse

Switch between perspectives using the Perspective bar in the upper right hand corner of the workbench

Views in Perspectives can be dragged/dropped to any area of the perspective

Additional views can be added via the menu “Window > Show View”

Page 40: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Navigating Eclipse

Views can be removed from a perspective by click on the “X” in the upper right corner of a view

Any View (Including Editor Windows) can be Maximized/Restored by double clicking on the tab that displays the view/editor title

If you totally FUBAR a perspective you can always restore the view to the original layout via the menu “Window > Reset Perspective”

Tip

Page 41: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Navigating Eclipse

The default Java PerspectivePerspective

Bar

Editor

Views

Page 42: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Creating Projects

The type of Project that can be created in Eclipse is based upon the features that have been added

“Out of the Box” Eclipse can only create the following project types Java Plug-in Resource CVS (Concurrent Versioning System – Code Sharing)

With Additional Features from the Eudora or other Eclipse/Third Party add ins Web

Static Web Project Dynamic Web Project

J2EE Enterprise Application Project Application Client Project

Page 43: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Creating Projects

All projects are created via the New Project Wizard File > New Project

Page 44: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Eclipse Workbench - Demonstration

Page 45: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Agenda

Roadmap

Eclipse, The Tool

Java language Fundamentals

Developing Java code In Eclipse

Develop and Deploy Lotus Domino Java code using the Eclipse development platform

Wrap Up

Page 46: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Java History

Originally was called the “Oak”

Was intended to be used in consumer electronics Platform independence was one of the requirements Based on C++, with influences from other OO languages (Smalltalk,

Eiffel …)

Started gaining popularity in 1995 Renamed to “Java” Was a good fit for the Internet applications

Page 47: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Portability

Java is a platform independent language Java code can run on any platform Promotes the idea of writing the code on one platform and running it on any

other

Java source code is stored in .java files

Compiler compiles code into .class files The compiled code is the bytecode that can run on any platform Bytecode is what makes Java platform independent

Bytecode is not machine code

Java Virtual Machine (JVM) Platform specific - Processes bytecode at runtime by translating bytecode into

machine code

Page 48: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Memory Management

Automatic garbage collection is built into the language No explicit memory management is required Occurs whenever memory is required Can be forced programmatically Garbage collector frees memory from objects that are

no longer in use

Page 49: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Distributed Systems

Java provides low level networking TCP/IP support, HTTP, and sockets

Java also provides higher level networking Remote Method Invocation (RMI) is Java’s distributed protocol

Used for communication between objects that reside in different Virtual Machines

Commonly used in J2EE (Java 2 Enterprise Edition) Application Server

CORBA could also be used with Java

Page 50: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Identifiers

Used for naming classes, interfaces, methods, variables, fields, and parameters

Can contain letters, digits, underscores, or dollar-signs

There are some rules that apply: First character in the identifier cannot be a digit

Can be a letter, underscore, or dollar sign Literals true, false, and null cannot be used Reserved words cannot be used

And it is Case Sensitive!

Page 51: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Expressions

Statements are the basic Java expressions Semicolon (;) indicates the end of a statement

HomePolicy homePolicy;double premium; premium = 100.00;homePolicy = new HomePolicy();homePolicy.setAnnualPremium(premium);

variable declaration

variable assignment

object creation

message sending

Page 52: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Comments

/** Javadoc example comment. * Used for generation of the documentation. */

/* Multiple line comment. * */

// Single line comment.

Page 53: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Literals

Represent hardcoded values that do not change

Typical example are string literals When used, compiler creates an instance of String class

String one = "One";String two = "Two";

String one = new String("One");String two = new String("Two");

Page 54: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Java and Types

There are two different types in Java Primitive data type Reference type

It is said that Java is a strongly typed language Fields, variables, method parameters, and returns must

have a type

double premium; HomePolicy homePolicy;

variable type

variable name

Page 55: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Primitives

Primitives represent simple data in Java

Primitives are not objects in Java Messages cannot be sent to primitives Messages can be sent to other Java objects that

represent primitives These are known as wrapper objects in Java (such as Double,

Integer, and Boolean)

Page 56: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Primitive Types

Keyword Size Min value Max value

boolean true/false

byte 8-bit -128 127

short 16-bit -32768 32767

char 16-bit Unicode

int 32-bit -2147483648 2147483647

float 32-bit

double 64-bit

long 64-bit - 9223372036854775808 9223372036854775807

Page 57: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Manipulating Numeric Types

A lesser type is promoted to greater type and then the operation is performed

A greater type cannot be promoted to lesser type Assigning double value to int type variable would result in a compile

error

12 + 24.56 //int + double = double

int i = 12;double d = 23.4;i = d; Type mismatch

Page 58: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Type Casting

Values of greater precision cannot be assigned to variables declared as lower precision types

Type casting makes primitives change their type Used to assign values of greater precision to variables declared as

lower precision e.g., It’s possible to type cast double to int type

int i = 34.5; //compiler error - type mismatchint i = (int)34.5; //explicit type casting

Page 59: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Reference Types

Reference types in Java are class or interface They are also known as object types

If a variable is declared as a type of class An instance of that class can be assigned to it An instance of any subclass of that class can be assigned to it

If a variable is declared as a type of interface An instance of any class that implements the interface can be

assigned to it

Page 60: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Reference Types (cont.)

Reference type names are uniquely identified by: Name of the package where type is defined (class or interface) Type name

This is full qualifier for the type and it is full qualifier for class or interface

java.lang.Objectcom.kiddcorp.demo.models.Policy

Page 61: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Object Operators

Keyword Description

instanceof object type

!= not identical

== identical

= assignment

Page 62: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Creating Objects in Java

In Java, objects are created by using constructors

Constructors are methods that have the same name asthe class They may accept arguments mainly used for fields initialization If constructor is not defined, the default constructor is used

Use “new” before class name to create an instanceof a class

HomePolicy firstPolicy = new HomePolicy();HomePolicy secondPolicy = new HomePolicy(1200);

Page 63: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Assignment

Assigning an object to a variable binds the variable to the object

HomePolicy firstPolicy = new HomePolicy(1200);

HomePolicy firstPolicy = new HomePolicy(1200);

HomePolicy secondPolicy = new HomePolicy(1200);

firstPolicy = secondPolicy;

1200

aHomePolicy

1200

aHomePolicy

1200

aHomePolicy

1200

aHomePolicy

Page 64: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Identical Objects

Operand == is used for checking if two objectsare identical Objects are identical if they occupy the same memory space

int x = 3;int y = 3;x == y; //true

HomePolicy firstPolicy = new HomePolicy(1200);

HomePolicy secondPolicy = new HomePolicy(1200);

firstPolicy == secondPolicy; //false

3

1200

aHomePolicy

1200

aHomePolicy

Page 65: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Identical Objects (cont.)

Variables that reference objects are compared by value Objects are identical if their memory addresses are the same

Variables are identical if they refer to the exact same instance of the class

HomePolicy firstPolicy = new HomePolicy(1200);HomePolicy secondPolicy = firstPolicy;firstPolicy == secondPolicy; //true

1200

aHomePolicy

Page 66: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Equal Objects

Determined by implementation of the equals() method Default implementation is in the Object class and

uses == (identity) Usually overridden in subclasses to provide criteria

for equality

HomePolicy firstPolicy = new HomePolicy(1200,1);

HomePolicy secondPolicy = new HomePolicy(1200,1);

firstPolicy.equals(secondPolicy); //false

Page 67: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Null

Used to un-assign object from a variable Object is automatically garbage collected if it does not

have references

When a variable of object type is declared, it is assigned “null” as a value

String one = "One";one = null;one = "1";

HomePolicy policy;policy = new HomePolicy(1200);…if (policy != null)

System.out.println(policy.toString());

Page 68: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Arrays

Arrays store objects of specific type One array cannot store objects of different types – String and int, for

example

When creating an array explicitly, its size mustbe specified This indicates the desired number of elements in the array Elements in the array are initialized to default values

Creating and initializing arrays at declaration

int arrayOfIntegers[];arrayOfIntegers = new int[5];

int[] arrayOfIntegers = {1,2,3,4,5};

The first element in the array is at the zero index

Page 69: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Arrays (cont:)

If not using initializer, an array can be initialized by storing elements at the proper index

An element of the array is accessed by accessing the index where the element is stored

int[] arrayOfIntegers;arrayOfIntegers = new int[3];arrayOfIntegers[0] = 1;arrayOfIntegers[1] = 2;arrayOfIntegers[2] = 3;

int[] arrayOfIntegers = {1,2,3,4,5};System.out.println(arrayOfIntegers[2]);

3Console

The length property will return the number of elements in the array

Page 70: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Conditional Statements

x ? y : z x must be type boolean, y and z can be of any type Evaluates to y if x is true, otherwise evaluates to z

if, if-else Can be nested Can omit braces if only one statment

(1 == 1) ? 7 : 4 //evaluates to 7

if(condition){statement1;

}else{ statement2;}

Page 71: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Conditional Statements (cont:)

Switch Statement Break Allows you to stop after executing an expression

switch(expression){ case value1: expression1; case value2: expression2;

expression3; default: expression4;}

switch(expression){ case value1: expression1;

break; case value2: expression2;

break; default: expression3;}

Page 72: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Looping

for loop

while() and doWhile()

for(int i=1; i<5; i++){System.out.println("Index is equal to " + i);}

Index is equal to 1Index is equal to 2Index is equal to 3Index is equal to 4

Console

int i=1;while(i < 5){

System.out.println(i);i++;}

int i=1;do{

System.out.println(i);i++;

}while(i < 5);

Page 73: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Java Fundamentals

Page 74: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

How to Define a Java Class?

Java class is defined by using the “class” keyword Class name follows the keyword, and by convention starts with capital

letter For example Policy, Client, House, etc.

Class access level must be specified before theclass keyword Public

Identifies that any other class can reference defined class Not specified

Identifies that only classes defined in the same package can reference defined class

It is the default access level modifierpublic class Policy{

…}

Page 75: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

.java Files

Java classes are contained in .java files One file can contain one public class One file can contain more than one non-public class

The file name is the same as the class name contained in the file

package com.kiddcorp.demo.models;

public class Policy{…

} Policy.java

Page 76: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Package Package groups related classes

Classes are usually related by their functionality, for example, domain classes, testing classes, etc.

Package is identified using “package” keyword

Package is a unique identifier for a class Two classes with a same name cannot be in the same package Different packages can contain the same class names

Compiled Java classes from the same package are compiled into the same directory The directory name matched the package name

package com.kiddcorp.demo.models;

Page 77: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Referencing Classes

A class must be fully referenced every time when used outside of its package

Full qualifier for a class is used package name + class name

package com.kiddcorp.demo.tests;

public class PolicyTester{com.kiddcorp.demo.models.Policy policy;…policy = new com.kiddcorp.demo.models.Policy();

}

Page 78: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Import Statement

Used to identify which classes can be referenced without fully identifying them Specified with “import” keyword Can specify a class, or all classes from a package

package com.kiddcorp.demo.tests;import com.kiddcorp.demo.models.Policy;

public class PolicyTester{Policy policy;…policy = new Policy();

}

Page 79: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Classpath and .jar Files

Classpath allows Java Virtual Machine to find the code CLASSPATH environment variable is used to indicate the root of where

packages are Packages are subdirectories under the root

Compiled Java classes can be packaged and distributed in Java Archive (.jar) files Packages in the .jar file are replaced with directories Essentially a zip file with a different extension

Page 80: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Defining Fields

A field definition consists of: Access modifier Field type Field name

package com.kiddcorp.demo.models;

public class Policy {private Client client;private String policyNumber;private double premium;

}

Page 81: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Initializing Fields

Fields are initialized when a new instance of a classis created

Primitive type fields get a default value Numeric primitives are initialized to 0 or 0.0 Boolean primitives are initialized to false Reference type fields are initialized to null, as they do not yet

reference any object

Fields can also be explicitly initialized when declared

Page 82: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Initializing Fields Explicitly

Possible when declaring fields

Not commonly used Constructors are generally used for initializing fields

package com.kiddcorp.demo.models;

public class Policy {private Client client = new Client();private String policyNumber = "PN123";private double premium = 1200.00;

}

Page 83: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Field Access Modifier

There are four different modifiers: Public

Allows direct access to fields from outside the package where the class is defined

Protected Allows direct access to fields from within the package where the

class is defined Default

Allows direct access to fields from within the package where class the is defined and all subclasses of the class

Private Allows direct access to fields from the class only

Page 84: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Defining Methods

Methods are defined with: Access modifier – same as for fields Return type Method name Parameters – identified with type and name

To allow access to private fields, getter and setter methods are commonly used

package com.kiddcorp.demo.models;

public class Policy {…public void setClient(Client aClient){…}

}

Page 85: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Constructors

Special methods used for creating instances of a class: Access modifier Same name as the class Manipulate new instance

package com.kiddcorp.demo.models;

public class Policy {…public Policy(){

setClient(new Client());setPolicyNumber("PN123");setPremium(1200.00);

}}

Page 86: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

What Are Static Fields?

Static fields represent data shared across all instances of a class There is only one copy of the field for the class

Static fields are also known as class variables

Java constants are declared as static final fields Modifier final indicates that field value cannot be changed

public class Count{public static String INFO = "Sample Count Class";public final static int ONE = 1;public final static int TWO = 2;

}

System.out.println(Count.ONE);1

Console

Page 87: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Static Methods

Define behavior related to the class, not individual instances Defined by using the “static” keyword Commonly utilitarian in nature (See the Wrapper Classes)public class Count{

private static String INFO = "Sample Count Class";public final static int ONE = 1;public final static int TWO = 2;public final static int THREE = 3;public static String getInfo(){

return INFO;}

}

System.out.println(Count.getInfo());Sample Count

Class

Console

Page 88: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Defining Inheritance In Java, inheritance is supported by using

keyword “extends” It is said that subclass extends superclass If class definition does not specify explicit superclass,

its superclass is Object class

Multiple inheritance is not supported in java

public class Policy{…public class HomePolicy extends Policy{…public class AutoPolicy extends Policy{…public class LifePolicy extends Policy{…

public class Policy{… public class Policy extends Object{…

Page 89: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Specialization and Generalization

ItemTextDateTimeValue

parseXMLcontainsValue…

RichTextItemEmbeddedObjects

embedObjectendSection…

Gen

era

lizati

on

Sp

ecia

lizatio

n

Page 90: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

What Is Inherited?

In general, all subclasses inherit from superclass: Data Behavior

When we map these to Java, it means that subclasses inherit: Fields (instance variables) Methods

Page 91: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Method Overriding

If a class defines the same method as its superclass, it is said that the method is overridden Method signatures must match

Consider Animal class: Dog and Cat as subclasses

All Animal objects should know how to talk

Animal aCat;aCat.talk();

Animal aDog;aDog.talk();

meows

barks

Animal

talk()

Dog

talk()

Cattalk()

Page 92: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Interfaces

Interfaces define a type and protocol, but do not provide implementation of that protocol Classes that implement interfaces must provide implementation for

defined protocol Interfaces capture common behavior of the classes that implement

them

Commonly used to: Impose protocol on set of classes that implement interface Indicate certain Java type (marker interfaces)

Interfaces are also stored in .java filespublic interface Animal{

public void speak();}

public interface Mammal{}

Page 93: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Interface Rules

Interfaces can contain Only method signatures Only final static fields

Interfaces cannot contain Any fields other than final static fields Any static methods Any method implementation Any constructors

It is possible that one interface extends other interfaces Multiple inheritance is allowed with interfaces

Inheritance works the same as with classes

Page 94: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Implementing Interfaces

Classes implement interfaces Keyword “implements” is used They must define all methods that the interface they implement

declares

public class House extends Building implements Policyable{public Policy createPolicy(){

HomePolicy policy = new HomePolicy(this);return policy;

}}

Page 95: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

What Are Collections?

Collections are Java objects used to store, retrieve, and manipulate other Java objects Any Java object may be part of a collection, so collection can contain

other collections

Unlike arrays, collections do not store primitives

Unlike arrays, collections can store different objects Not all objects in a collection must be the same Java type

Page 96: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Most Commonly Used Collections

The most commonly used collections include: Collection framework collections:

ArrayList HashMap HashSet

Legacy collections re-implemented with the collection framework: Vector Hashtable

Page 97: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

What Is an Exception?

Exceptions are unexpected conditions in a program Like Opening a file that does not exist Or Sending a message to an object that object does

not understand

In Java, there are two different types of exceptions Unchecked exceptions

Runtime exceptions and errors Checked exceptions

All others exceptions

Exceptions are objects in Java, and they are all of the Throwable type

Page 98: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Generics

New Java 1.5 is the concept of the Generic

In the old days, anything put into a collection was stored as a generic Object

Thus when you retrieved it from the collection you had to cast it to the appropriate type

ArrayList al= new ArrayList();al.add(new Document());

/*Later in the code we retrieve the value */Object obj=al.get(0); //Value is retrieved as an objectDocument doc=(Document)obj; //Cast to the appropriate type

Page 99: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Generics Save the Day

No longer is it necessary to cast the value retrieved from a collection to it’s real type

This provides significant improvements in code style and type safety

ArrayList<Document> al= new ArrayList<Document>();al.add(new Document());

/*Later in the code we retrieve the value */Document doc=al.get(0); //No need to cast

Page 100: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Try – Catch and Finally Blocks

Exceptions are handled in a try-catch block

Finally Always executes at the end after the last catch block Commonly used for cleaning up resources (closing files, streams, etc.)

public void myMethod(){try{

//code that throws exception e1//code that throws exception e2

}catch(MyException e1){//code that handles exception e1

}catch(Exception e2){//code that handles exception e2

}finally{//clean up code, close resources

}}

Page 101: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Java OO Fundamentals

Page 102: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Agenda

Roadmap

Eclipse, The Tool

Java language Fundamentals

Developing Java code In Eclipse

Develop and Deploy Lotus Domino Java code using the Eclipse development platform

Wrap Up

Page 103: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Java Development Tooling – JDT

Eclipse’s Java Development Environment is often referred to as Java Development Tooling (JDT) Using the JDT you can do the following things with

Java programs: Write Compile Test Debug

Remember the right click menu!

Page 104: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Perspectives

When developing Java code, commonly used perspectives include: Java Perspective

Designed for working with Java projects Java Browsing Perspective

Designed for browsing the structure of Java projects Java Type Hierarchy Perspective

Designed for exploring type hierarchy Debug Perspective

Designed for debugging Java programs

Page 105: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Java Perspective

Contains: Outline View Editor area Package Explorer View Hierarchy View Tasks View

Page 106: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Java Browsing Perspective

Contains: Editor area Projects View Packages View Types View Members View

Page 107: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Java Type Hierarchy Perspective

Contains editor area and Hierarchy View

Page 108: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Build Path Project Preferences

You can set global preferences for a project Select Window → Preferences to

get Preferences View Good idea to separate your Java

files into source and compiled directories (src and bin)

This action only needs to be done once

Done for all subsequent projects

Tip

Page 109: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Creating a Java Project

Projects are used to organize resources (source, class, icons) for a project

To create a Java project: Select Window → New → Project …

from the menu The New Project wizard

comes up Select Java → Java Project Click Next

Page 110: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

New Project Wizard

Specify a Project name

Click Next

Page 111: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

New Project Wizard: Java Settings

Specify Java Settings Output folder (where compiled

files will be stored) Any external JAR files the project

depends on Classes from other projects that

are referenced in the project

Click Finish

Page 112: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Project Properties

You can change the Java build path at any time Choose Properties from the context menu on the project

Page 113: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Creating Packages

A package contains Java class files

To create a package for a project: Select the project Choose New → Package from the

context menu Specify package Name Click Finish

Page 114: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Creating Classes

To create a class in a package: Select the package Choose New → Class from the

context menu The Class wizard comes up Specify class details Click Finish

Page 115: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Using Code Assist

When activated, code assist opens a list of available code completions

Code Assist activates by Crtl+Space Activates automatically when a message needs to be sent to an object

(after the dot is typed)

Tip

Page 116: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Using Quick Fix (Ctrl + 1)

Useful if Java compiler shows errors Gives options for fixing the errors Activated through Edit → Quick Fix menu option

Error indication

Page 117: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Searching for Java Classes

When developing Java applications, a good search mechanism is very important You often search for class, method declarations,

and references It is important to be able to find things quickly

Eclipse Java Search mechanism is very extensive

It allows you to search for: Types, methods, constructors, packages, and fields Declarations, Implementers, References In the scope of Workspace, Working Set, or

Selected Resources

Page 118: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Organizing Java Code

Eclipse comes with extensive support for organizing and refactoring Java code

It is possible to: Generate getters and setters for the fields Organize missing import statements Move fields, methods, classes Rename methods, classes, packages

Page 119: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Generating Getters and Setters

Available for creating get and set methods on the fields It supports encapsulation activated by choosing Source → Generate

Getter and Setter from the editor’s context menu

Page 120: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Source Navigation F3

Navigate to a type, method, or field at the current cursor location

Ctrl+Shift+T Open an editor for any Java type that is available

in your workspace. Also via menu Navigate → Open Type … or using a toolbar icon

Ctrl+O Open a lookup dialog containing a list of

members in the current Java editor. Start typing characters to limit the list and press Enter to go to the selected member. A quick alternative to the Outline view.

Ctrl+F3 Open a member lookup dialog for the class

name at the current cursor location, e.g., position on Date and press Ctrl+F3 to review and lookup one of its methods

Page 121: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Source Code Templates

Provides a “shorthand” for common programming tasks

Page 122: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Eclipse JDT

Page 123: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Agenda

Roadmap

Eclipse, The Tool

Java language Fundamentals

Developing Java code In Eclipse

Develop and Deploy Lotus Domino Java code using the Eclipse development platform

Wrap Up

Page 124: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

Java versions by Domino release ND 8

Java 5 (1.5) / with notes.ini variable set

– JavaCompilerTarget=1.5

– Notes/Domino will compile agents using

– javac -source 1.5 -target 1.5

Java 5 (1.5) / without notes.ini varibale set (Default)

– Notes/Domino will compile agents using

– javac -source 1.3 -target 1.2

Page 125: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

Java versions by Domino release (cont) ND 7

Java 1.4.2 ND 6

Java 1.3.1 ND 5

Java 1.1.8

Page 126: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

If you attempt to compile code using Java features not supported by the runtime you will receive compile errors

The following is an example of running Java 1.5 specific code in Notes without the .ini variable set

Page 127: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

The JVM in ND 8 is enabled with a Just-in-Time compiler or JIT

Using JIT compiling will improve performance in the execution of Java code running on the ND 8 platform

JIT is enabled by default Although it is not recommended you can turn off the JIT using the

following .ini variable JavaEnableJIT=0

Page 128: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

The Problem The Domino Designer client does not include support for Java code

completion or Pop-up syntax help

The Solution Write your Java agents in a Java IDE !!

Eclipse Rational Developer Any IDE that lets you access the .java file from the file system

Page 129: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

Steps for Developing and Deploying Domino Java Code Create a Java Project in Eclipse Create Agent source code in Project Create a new Agent in the Designer Client Import source code from Eclipse into new Agent

Page 130: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

Create a New Java Project in Eclipse File > New Project > Java Project

Provide a Project Name Select the JRE

– If you have configured Domino as one of your JREs in the workbench preferences you can select it here

Project Layout

– Keep separate source and output folders Click “Next”

Page 131: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

In the Java Settings Dialog Click on the “Libraries” tab. This is your projects “Classpath”

This will allow you to add additional libraries to your project

– If you are NOT using the Domino 7 or 8 JRE you will need to add the “Notes.jar” file here

Click “Add External Jars”

– Navigate to the proper Domino Directory Domino 6

– C:\installdir\notes Domino 7

– C:\installdir\jvm\lib\ext Domino 8

– C:\installdir\jvm\lib\ext This is also where you would add third party

jar files to your project Like JDBC drivers, etc

Page 132: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

The majority of Java Agents are created without a package reference Although it’s a good idea to start getting in the habit of using them Packages are typically the reverse DNS name of your organization

com.acme.code

In the “src” folder of your Java Project Create a package or use the “default package” Create a new Java Class

Right click package or folder and choose “New > Class” from context menu

This will open the “Java Class” wizard

Page 133: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

Provide a Class name in the Name fieldDo not use the value of “JavaAgent” that is used by default in all new Java Agents in the Domino Designer Client

Choose a name that is indicative of the agent functionality

Do not worry about any of the other settings

Click the “Finish” button to create the class and open the source code in the Java Editor

Page 134: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

The code created by the wizard will need to be replaced with template code created from a new agent in the Designer Client You can

Cut / paste the new agent template code from the Designer Client Create a “Snippet” in Eclipse that stores the new agent template

code Use Plug-in provided (See resources at end of presentation)import lotus.domino.*;

public class JavaAgent extends AgentBase {

public void NotesMain() {

try {Session session = getSession();AgentContext agentContext = session.getAgentContext();

// (Your code goes here)

} catch(Exception e) {e.printStackTrace();

}}

}

Page 135: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

Every newly created agent in the Designer Client creates a new source file with a class name of “JavaAgent”

When the template code is added in eclipse this name will need to be “re-factored” to the name of the class created in Eclipse

Click on the “light bulb” and choose “Rename type to…”

Page 136: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

Write the Java code in Eclipse to complete the agent functionality

Save the source code and ensure that there are no compile errors

Toggle back to the Designer Client Create a new Java Agent and give it the same name as the class in Eclipse Click the “Edit Project” button on the bottom of the agent IDE in the Notes

designer client Change the “Base Directory” to point to the source folder of your eclipse project

You will only have to do this once per database Add the source (.java NOT the .class) file to the Current Agent files One time only

Change the “Base Class” to the new source file name Delete “JavaAgent.java” from the file list

Click “OK” to import the source code

Page 137: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

Page 138: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

The first time the source code is imported the code block will be collapsed Click the “+” to expand it

When changes are made to the source code in Eclipse Click the “Edit Project” button Click on the “Refresh All” button Click “OK”

You will be prompted that the source code will be replaced with the new code Click “Yes” to accept the new code changes

Page 139: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

Using this procedure Make sure you ALWAYS make your code edits in Eclipse Import the changes to Existing/New agents in the Notes Designer

Client

Page 140: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code - DEMO

Page 141: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

Java Domino Objects Only implements the “Back End” objects Exposed via:

lotus.domino.* No Notes Client UI access You (the Domino Developer) already know 98.67 percent of all of the

Java class names for the Domino Back End Objects Drop “Notes” from the front of the LotusScript class names and you

have the Java class names

LotusScript Class Name

Java Class Name

NotesSession Session

NotesDatabase Database

NotesView View

NotesDocument Document

NotesItem Item

Page 142: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

Performance Java Agent vs. LotusScript Agents

Java Agents and LotusScript Agents call the same C++ code base On an “Apples to Apples” comparison they will perform equitably

Classes the don’t exist in LotusScript AgentBase AppletBase and JAppletBase NotesError NotesException NotesFactory NotesThread

Page 143: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

AgentBase Java Agents MUST extend AgentBase Java Agents MUST use the NotesMain() method as the entry point for

their functional code The getSession() method creates a Session object

import lotus.domino.*;

public class MyFirstAgent extends AgentBase {

public void NotesMain() {try {

Session session = getSession();AgentContext agentContext = session.getAgentContext();

// (Your code goes here)

}catch(NotesException ne){ne.printStackTrace();

} catch(Exception e) {e.printStackTrace();

}}

}

Page 144: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

Primary problems LotusScript Developers have transitioning to Java Compile Errors

Java is Case Sensitive

– Yes, it’s always going to be case sensitive Data typing errors

– Java has no “Variant” data type Most common runtime Errors

Null Pointer exception Class not found exception

Page 145: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

Domino Java agents have no access to the Notes UI

System.out.println() commands can be inserted into the code Output from agents is written to the “Debug Console” from code

executed on the client Output from agents is written to the Server Console from code

executed on the server

Access the Debug Console via the menu “File > Tools > Show Java Debug Console” from the Notes/Designer client

Page 146: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Develop and Deploy Lotus Domino Java Code

Code Walk Through Session AgentContext Database View Documents – Read

Help Doc Count Agents Documents – Create Items – Read Items – Create Run on Server Java Network I/O HTML Output XML Output Web Query Save

Page 147: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Wrap Up

Follow the “Roadmap” J2SE Servlets, JSPs, JSFs XML Rest of J2EE Specification

Start with Java Agents in Domino

Transition to Servlets running on the Domino Server

Transition to external Servlet/JSP – J2EE server

Avoid the Roadblocks !

Introduce yourself to Plug-ins and the Eclipse Rich Client Platform

Page 148: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Other Java Related Sessions you should checkout

AD201 - The Amazing IBM Lotus Notes 8: Extendable with Plug-ins

AD203 - Developing Secure Java Applications in IBM Lotus Notes 8

AD301 - IBM Lotus Domino Designer

AD308 - Java 5.0 for IBM Lotus Notes and Domino

AD312 - Using Eclipse To Develop, Debug and Test Java Agents

HND102 – IBM Lotus Domino Designer in Eclipse

HND103 – Developing Eclipse Plug-ins to Extend IBM Lotus Notes 8

Page 149: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Wrap Up

References and Resources NNSU

Domino Eclipse/RAD plug-in http://www.nnsu.com

TLCC Java Computer based training via Notes Databases http://www.tlcc.com

O’Reilly Publishing Head First… Series http://www.oreilly.com

SUN Developer Network http://java.sun.com

Eclipse Project http://www.eclipse.org

Page 150: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Wrap Up

References and Resources (cont) Apache Jakarta Project

http://jakarta.apache.org Developer Works

http://www.ibm.com/developerworks http://www.ibm.com/developerworks/java http://www.ibm.com/developerworks/lotus http://www.ibm.com/developerworks/xml

Page 151: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

Remember the On-line Evaluations !!!!!

Please take a moment after this session to fill out the on-line evaluations !!!!!!

Page 152: ® It Takes Two to Tango – Java and Eclipse John Kidd – Kiddcorp, L.P. Paul T. Calhoun – NetNotes Solutions Unlimited, Inc JMP207

You got Questions? We got Answers !

[email protected] [email protected]