Earlier today myself, David Leedy, John Jardin and Jeremly Hodge were discussing Java integration with XPages. Particularly with the introduction of the new Java design element, developers are starting to try Java. For those – myself included – who have come from a traditional Domino development background some aspects come more naturally than others. The Domino object model is very familiar, particularly after Server-Side JavaScript. And Tim Tripcony’s class in dPloy on OpenNTF makes it easy to get a handle on parts of the XPages runtime. Arrays are familiar to Domino developers; Tree Maps are roughly analagous to Lists; and developers who have worked with classes in LotusScript will find the structure of classes in Java fairly similar.
But other aspects are more challenging. The questions discussed were around using a method from a class in Server-Side JavaScript and keywords like static, public, private and final. Thanks to Jeremy Hodge for enlightening us and, as a result, our practices will be improved.
First of all public, private etc. A property or method can be private, public or protected. This defines the scope to which it is available. Private and public will be familiar to anyone who has worked with classes. Private means you can only reference this property or method from within the class. Public means you can access it outside the class. But protected is new to LotusScript developers. Protected means it’s only accessible to this class or a class that extends this class. So if a class BasePerson has a protected property called fullName, it can be accessed by a class ExtendedPerson which extends BasePerson. But it cannot be accessed from a class BaseCompany. The same holds true for methods. But typically properties will be private, but the getter and setter methods for them will be public. So you will see something like this:
public class BasePerson {
private String FullName;
public getFullName() {
return FullName;
}
public setFullName(String fullName) {
FullName = fullName;
}
}
An added keyword is final. Final is used to say this is what the class member (whether a property or a method) is going to be and it’s never going to change. It improves optimisation in the compiled code. It doesn’t mean the value is constant, just that it’s never going to be reassigned. So for example:
private final TreeMap
means that tm is always going to be that object, so you can’t anywhere else say:
tm = new TreeMap
Where the conversation really began was with static. In a LotusScript context, a class tends to be non-static, because you create an instance of the class and access its properties and methods through that object. If you have a class BasePerson, you don’t tend to call BasePerson.getFullName. Instead you create a new BasePerson called myPerson and call myPerson.getFullName. Subs and Functions in a Script Library are like static in Java, in that you don’t create an instance of the Script Library, you just use the script library and call the relevant Sub or Function.
In Java Domino objects are not static. You have to create a new Session object to call the getDatabase() method. You can’t just call Session.getDatabase(). But classes like Integer and Double have static methods. If you have a variable that’s an int and you want to convert it to a String, you call the static method toString(int) directly on the Integer class, so it’s Integer.toString(myInt). It’s not called on an instance of the class. It’s the same with static methods and properties in your own classes.
So if the BasePerson class in your com.domain.packageName package has a static method getFullName(), the way to call that in Server-Side JavaScript will be:
importPackage(com.domain.packageName)
BasePerson.getFullName();
If the method is not static, you need to create an object first. So the code will be:
importPackage(com.domain.packageName)
var myPerson = new BasePerson();
myPerson.getFullName();
The method is called on an instance of the class rather than the class itself (and so the class will need a public non-static method to instantiate the class – public BasePerson() {}
Static methods will effectively be used for generic scripts for reusable functions, such as a getApplicationScope. If you read my blog post of the 1500 Java packages in Domino you’ll know I referenced org.apache.commons.lang. This has a StringUtils class which has static methods including substringAfter. The syntax to use is StringUtils.substringAfter("Label~Value", "~"). You don’t call:
String myStr = new StringUtils;
myStr.substringAfter("Label~Value", "~")
The impact then is that any variables in your class referenced by the static method also have to be declared as static. So if getFullName() is static and references the FullName property as above, that must be declared as private static String FullName.
Thanks to Jeremy for clarifying things, it’s certainly clearer to me now.
Actually Protected means accessible:
– from the same class
– from any class in same package
– from a subclass in the same package
– from a subclass outside the same package
but not
– from any non-sub-class class outside the package
brgds Jesper Kiaer
http://nevermind.dk
might as well take the others too 🙂
Private means accessible:
– from the same class
but not
– from any class in same package
– from a subclass in the same package
– from a subclass outside the same package
– from any non-sub-class class outside the package
Public means accessible:
– from the same class
– from any class in same package
– from any non-sub-class class outside the package
– from a subclass in the same package
– from a subclass outside the same package
Default means accessible:
– from the same class
– from any class in same package
– from a subclass in the same package
but not
– from a subclass outside the same package
– from any non-sub-class class outside the package
In Java an object can not be Static, but methods and variables can be.
just to clearify 🙂
brgds Jesper Kiaer
http://nevermind.dk
Thanks, I love nice clear, concise explanations like that. Thanks for taking the time.
Great stuff, Paul. I hope it will help a lot of people get more comfortable with Java.
I’ll add my own points of clarification…
The analog to Lotusscript’s List is Java’s Map. TreeMap is a version of Map, but unlike List, it’s n-tier hierarchical, so the closest implementation to LS List is, in my opinion, HashMap.
Java has a native notion of arrays by simply using square bracket notation. String [] is an array of Strings. However, it’s a major pain to dynamically expand and contract them, so unless you have very specific performance optimization needs, you’re almost always better off working with an ArrayList. The ArrayList class is very fast, has been highly optimized for runtime performance, and really disposes of the size management hassles that plague native arrays (and Lotusscript arrays, for that matter!)
Jesper is correct that methods and variables can be static in Java. Also so can INNER classes (a concept that will take some getting used to for LS developers.)
Static is really Java’s implementation of procedural functions. Static is without specific object context, which means that all the things a static routine needs to know must be given to it as parameters. (Okay, for the pedantic, that isn’t strictly true, but I’m not going to divert into a discussion on ClassLoaders…)
For Lotusscript programmers, think of any sub or function you might write in a library. For instance, a sortArray() function you might put into a library. You’d typically write something like…
Function replaceSubstring(origString As String, sourceString As String, replaceString As String) As String
In Java, you’d implement something like this as a Static method…
public static String replaceSubstring(String origString, String sourceString, String replaceString)
One might ask “why?” And that’s a great question. The answer is performance. Static methods, because they don’t require object construction, are almost always faster to execute. Of course, since they don’t have object context, there’s lots of bad things about them (mostly maintainability.)
As you’ve observed here, Paul, there’s an absolutely ridiculous number of available classes in the Notes/Domino contexts. Of those 1500 packages, there are any number that provide extremely useful static methods. Fortunately, most such classes will have a name that ends in “Utils.” So it’s not terribly difficult to find them.
Yes, HashMap is the right comparison. The difference with TreeMap is that it’s automatically sorted alphabetically on the key, which is why the example of full text searching on xSnippets uses a TreeMap.
The problem with HashMap is you don’t get the objects in the HashMap back in the order you put them in. For that, you need to use LinkedHashMap.
Ack, typo! The above should say…
“For instance, a replaceSubstring() function you might…”
I didn’t chose a sorting algorithm precisely because LS forces ambiguity of the return type.
For those who want to learn Java:
Read some book!
There is no better way to start with new language.
Please do not start learing from blogs or by hit-and-miss experiments with code.
What kind of book You ask?
Well maybe its enough to check out what community where actual java programmers can be found thinks:
http://programmers.stackexchange.com/questions/91629/best-java-book-you-have-read-so-far
A Java book is useful for Domino Developers as reference. Indeed my own bookshelf includes Head First Java. But my problem with using these books or reading them through cover to cover is that as a Domino Developer I’m writing beans or Java utilities. I’m not writing a standalone Java application. So the tutorials, while useful to read through, can’t be attempted in an NSF and thus the major benefits of these books is rendered useless to a Domino Developer. And the same is true when looking at Java for plugin development.
I’m beginning to think a different kind of book – more targeted at Domino, going through an NSF and on to plugins – may be of interest. I may need to make a New Year’s resolution if I feel brave enough (with more competent technical reviewers to keep me on the straight and narrow!)….
I did not say to read it cover to cover. Most of books may contain obsolete parts (like awt). Its up to You as a developer to choose the important parts. I do not claim that reading tutorial/articles on web is pointless – as a matter of fact its very important but without prior knowledge its hard to tell good from bad stuff apart. I wonder how many readers spotted “Protected means it’s only accessible to this class or a class that extends this class” statement except for Jesper. This was not unclear statement! This was simply false!
My point was that I would like to see more people in yellowverse with ability to write decent code and with understanding of what they are doing. And this can only come with vast organized knowledge. You know – the type of knowledge that can be found in good books. Pure practise without this is pointless.
Java Head,
While books have their place in learning, and I LOVE books, to just say to a domino developer go find a book about Java isn’t a recipe for success. Otherwise we’d have more Java developers. How does one even know what parts can be ignored or not when starting a new language?
But as Paul points out the biggest barrier, in my opinion, is it’s very murky on how to get started in Domino development to even play with Java. Especially XPages before 8.5.3 where we now have this new Java Design element in front of us. Sure we had Java Agents… Not a ton of books or tutorials on how to get started there… and obviously much less in the Xpages world.. which is completely different from Java Agents.. to this day the only true getting started in Java for Xpage developers that I know of is the videos Jeremy Hodge did for NotesIn9. We need more like that.. More videos and blog posts that help bridge the Domino developers with how to play around with Java inside an NSF context.
And I think there’s a LOT of people in the yellowverse writing decent code. They’re just doing it with the tools that they have. Just because they’re not using Java yet doesn’t mean they’re not building apps with decent code. Sure it would be nice if more people got on the Java “bandwagon”. Some of that is the developers fault maybe… for not reaching… keeping up. That’s fair to say. But when there’s little info or even reason to get into Java – then it’s that much more a barrier to entry.
But that might be changing. I know the Hodge videos helped a bunch of people get on the path. I myself am just starting – so there were be more information coming to notesIn9 VERY SOON actually. I do believe that in XPages development – all roads leads to Java really. I’m not happy about it, but I’m accepting of it.
All this being said – and I’ve not gotten to far yet – but I am liking the Head First Java book very much and highly recommend it.
@Paul, thanks for the reminder as to why I said “in my opinion.” You’re indeed correct that the in-order processing of LS List requires the use of the Java LinkedHashMap. In fact, we really should go one step further and point out that it would be a LinkedHashMap, because the keys in LS List are auto-cast to strings. myList(“1”) = “foo” assigns the same member value as myList(1) = “foo”
I couldn’t disagree more about getting a book. Notes/Domino developers are almost always pragmatic programmers, and very little about pure Java theory is going to help them. In fact, it will serve mostly to confuse. For instance, the discussion on the real visibility of protected variables and members, whether the specifics are strictly true or false, makes no substantial difference in practice. In fact, all object access restrictions can be overridden through privilege escalation and introspection. So if people want to be so pedantic as to call the original statement “false,” then we might simply claim all private/public/protected declarations to be syntactical sugar.
Except that wouldn’t be useful, and ultimately just confuses people trying to get productive work done. So let’s NOT do that.
What’s far more important than the formal rules and strict behavior of the Java language is: how do you use it effectively in the Notes/Domino context? Learning best practices will yield far more success than learning the nuances of, say, ThreadLocal variables.
The improvements in XPages and Domino 8.5.3’s Java design element will increase, I am sure, the prevalence of Domino developers using Java. And I am sure the forthcoming year will show a proliferation of XPages-related Java resources (whether in terms of best practice Java books, blog posts, wiki articles, code snippets on xSnippets, or full applications like dPloy, XPages Help Application and presumably watrcooler, not to mention best practice / JumpStart / MasterClass sessions at LUGs and other conferences). Indeed I anticipate the proliferation of good quality reference material to be similar to that in the first couple of years of XPages development itself. Like XPages, there’s a learning curve.
I firmly believe there is a need and a desire from the community to ‘send the escalator down’, to quote Kevin Spacey from last year’s Lotusphere OGS
I understand what both of You is defending here. I can see from years of contact with online community that there is high probablity that most of domino developers are typical Wally-like (Dilbert anyone ?) workers (Warning: this statement is purly subjective). Its not that this is very bad. This can work when the environment is mature and well defined – SAP is good example of this.
But the problem here is that IBM left us with unfinished product with little or no support:
– one book (Mastering ..) after .. how long it was .. 3 years ? .. give me a break
– community driven wiki (When there is almost no community ? How exactly this is going to work ?)
– openntf projects like extension library which status is vague (Which part of it will go to the core xpages? Anyone wanna bet ?)
This means that the only way this is gonna work is strong and knowledgeable community.
And promoting lack of basic understanding of tools is not gonna help this.
“For instance, the discussion on the real visibility of protected variables and members, whether the specifics are strictly true or false, makes no substantial difference in practice”
You missed the point. I wanted to show only an example. It does not matter if this was about scope, proper use of finally clause or streams.
“I do believe that in XPages development – all roads leads to Java really. I’m not happy about it, but I’m accepting of it.”
You are not happy about this. Why? Could You please elaborate?
Please speak from a position of some knowledge.
In response to little or no support:
“one book”: two more are already available for pre-order on Amazon, including one on the Extension Library, written by members of the “(almost no)” community, including myself.
“wiki”: just browse that wiki and count the authors. The statement “little or no community” just doesn’t stand up to even the most cursory investigation.
“extension library which status is vague”: there was a community call earlier this week and the XPages podcast, The XCast, released yesterday which made it clear that within two weeks IBM are releasing a significant portion of the Extension Library only excepting the newer controls as part of Upgrade Pack 1. The position is not vague. It has not been vague to anyone paying attention to IBM’s message since the project was released.
You also forget a few other resources:
– Domino Designer Help, which on Domino-related content is vastly superior to most documentation I’ve come across. Clear context-sensitive help with concise, extremely useful examples.
– Countless blogs from the “(almost no)” community.
xpages.info site.
– NotesIn9 videos by David Leedy and the “(almost no)” community.
– Training courses from IBM and TLCC.
– Mentoring packages such as the one Intec offers.
– Lotusphere annually in January sponsored by the “(almost no)” community where thousands of people from the “(almost no)” community and 500 students interested in getting into the “(almost no)” community sponsored by GBS, a business partner in the “(almost no)” community.
– Lotus User Groups almost one a week worldwide and growing all the time, all packed and mainly sponsored by companies in the “(almost no)” community. Take it from one who has spoken at LUGs across Europe over the last two years, there is CONSIDERABLE appetite for XPages and a lot of developers getting involved. And as an example…
– Two OpenNTF competitions sponsored by We4IT with strong numbers of entrants from the “(almost no)” community. And that includes non-XPages Java developers seeing the benefit of providing their products to XPages developers. Just take a look at this article including the answer to the driving force behind entering the contest http://blog.zkoss.org/index.php/2011/09/04/exclusive-interview-with-ibm-xpages-development-contest-winner-dennis-chen/
My bad – I should not write “almost no community” because You are right that there are few people intrestet in this technology. I said few because I believe that this statistic is not impresive at all (roughly 4000 unique hits in more than a year with intrest going down in 2011 ? And almost no interest outside US and Germany?) :
http://notesin9.com/index.php/2011/11/09/xpages-interest-its-out-there
Development wiki number of authors is sligthly more impressive (I counted 🙂 ~120). But the number of xpages articles worth reading is not …
But what I really meant is that there is not enough people like Dennis Chen (did he write any articles/tutorials at all ?) in community. People who could share their deep understanding of the techlology. Unfortunately there is too much people who dont know basics of this technology (mainly because they are too pragmatic as You call it).
Due to this there is enough articles that introduces You to Xpages but there is only few that explains You the inner mechanics (I believe Dennis claimed in the interview exactly what I want to point out).
And unfortunatelly IBM f*d the technology by using old(1.1) jsf with lots of custom code so jsf community will not help You there. Especially since nobody there uses jsf 1.1 – this line is considered as a mistake just like ejb 2).
As to extlib could You please point me to the article about Upgrade Pack 1?
I must have missed it and I am very intrested in this since my company really need this information. Our applications are to complex (the interface) to be implemented in pure xpages and without ext lib we would forsake this technology (if not the whole platform). Belive me I would be really happy if IBM would give clear message about its plan.
And about the rest – I knew almost all what You showed here and I still belive that this is too little and too late (this is totally subjective!). Fortunately most of the competition is only sligthly better so there is a chance this technology can win in the end. But I would not bet on this.
In this end I belive You may have clearer view that I.
At least I hope You do!
Sorry, I was wrong, the community call has not yet happened. I had slightly advanced information on a Business Partner call, but Chris Miller was on the same call and created this post, including a screenshot of one of IBMs slides which makes their policy regarding OpenNTF and Upgrade Packs clear: http://www.idonotes.com/IdoNotes/idonotes.nsf/dx/8.5.3-upgrade-pack-1-being-released-what-it-offers-you.htm
The Community Call is on December 13th. Details can be found here. https://www-304.ibm.com/connections/blogs/socialbusiness/entry/ibm_collaboration_solutions_community_meeting_dec_13th_10am_et_us_get_social_with_notes_domino_8_5_36?lang=en_gb
For info, the majority of the speaking engagements I’ve had have been in Belgium, Netherlands and UK.
Ah, I see Paul’s blog stripped my generic reference. I’d attempted to type “LinkedHashMap[String, Object]” where the square brackets were replaced by less-than/greater-than signs.
Damn you, HTML content filtering!
Wow. We’ll I’m not looking to argue with someone who seems more interested in throwing stones then having actual discussion that’s not even related to the blog topic. Probably I’d point you to the XPages forum COMMUNITY resource for some of your points. But I’ll address a could of things that you’ve commented on.
You said: “I do believe that in XPages development – all roads leads to Java really. I’m not happy about it, but I’m accepting of it.”
Server Side JavaScript is a wonderful thing. It’s not TRUE JavaScript, it’s more like “ECMAScript” or I guess the syntactic of JavaScript. But in SSJS we can combine JavaScript, backend objects, JAVA objects and even the older @Formula syntax to create logic. Gosh that’s convenient. It provides that and still being very “JavaScriptish” which is needed for client side coding, that seems to me to be a great place to code. However there are disadvantages to SSJS. I’m not going to get into all my perceived cons, but suffice it to say even though I’d prefer to say in SSJS, I now think in the long run keeping business logic inside Java itself will be better off.
You bring up the stats that I published from 1 single NotesIn9 show. This show is a pure from scratch introduction. I publish that one because it likely has low re-watchability and is the best thing that I have to try and gauge NEW people coming into XPages. This is just 1 SMALL perspective. It’s my little website. Not everyone doing XPages has even found it you know. I can’t tell you how many times I’ve presented on XPages and many of the audience never even heard of NotesIn9. And as I said – those stats don’t include YouTube views and there’s likely other issues as well. It’s just the best barometer I have for my personal contributions.
Regarding quality of material. NotesIn9 focuses on beginning to intermediate material. There is quite a lot of advanced material out there. Anything from Tim Tripcony or Karsten Lehmann come to mind. But honestly much of that is over my head because it is just too advanced for me… for now. 🙂
IBM has a clear message about the “plan”. The ext library is going into the product in the form of upgrade packs. But it works now in the openNTF form so it’s not like you need to wait to get started. If your apps need the ext. library then by all means use it. That’s what it’s there for.
Sorry You are right this is totally OT. And all I wanted is to promote my believe that book are the best way to start learning new topic 🙂
I did not want to be perceived as someone who throws stones. I just wanted to express my feeling that the community is rather small (especially in my part of Europe).
But of course I can be wrong so I am glad to hear others opinions.
As for ssjs – I believe it is a really good thing and what You said is totally right (escpecially about being different than js). But I would like to see more articles on where it should be avoided at all costs.
Tim Tripcony or Karsten Lehmann are good bloggers. The stuff they do is amazing but still I could not find most answers to my questions. We need more people like them. People who are not always only pragmatic but sometimes try to understand more.
“The ext library is going into the product in the form of upgrade packs.”
Could you please show me source of this information. This is really important for me.
Regarding the upgrade pack. It was mentioned on chris millers blog not that long ago. He showed a slide from somewhere. This isn’t a secret. It’s probably been mentioned on Ed bills blog.
The next community meeting will be discussing it. https://events.webdialogs.com/portal/wipevents/register.php?id=a3084ebfe7&l=en-US