strategy for java jee development

take UI code debug and modification for example. previously, i have been tracing the log, using firebug check the URL address of the link, or better advanced using debug mode. But whatever method it is, i have been making huge effort on making sure, the 100% clear sure flow is made known. click button A —> to ActionA.class —> forward ServiceB.class —> DAOC.class –> JSPD.jsp.

it’s time consuming and effort consuming, yet, still times, there are mistakes. let’s say checking and follow every line, every detail of the code, then i finally modified A.class, D.jsp. Deploy and test, it might still wrong. Might be DAOD, need to be changed.

I just realized, after three years, knowing roughly the usual way of coding, structure, roughly architech, utilizing fiddler, click button A, button B, whatever, then check fiddler record, see what possibly what’s the action, let’s say ActionA.class, then go there, roughly check through, i would quickly decide possible problem occurrence place.

then deploy and test, it probably works.

conclusion is, now since now the “behind” coding (the why, how it works) I have roughly know, so I don’t any more spend so much time effort on tracing so detail, 100%. And utilizing fiddler, httpwatch professional, or debug mode, work is better and faster.

unlimited !

public class Outer2 {
        private int size;

        public Object makeTheInner(final int localVar) {
                final int finalLocalVar = 6;
                class Inner {
                        public String toString() {
                            return ("#< Inner size=" 
+ size +" localVar=" + localVar 
+" finalLocalVar=" + finalLocalVar + " >");

                return new Inner();

        public class Inner {
                public void doStuff() {


public class TestInner {
        public static void main(String[] args) {
                Outer2 outer = new Outer2();
                Object obj = outer.makeTheInner(1);
                // Must create an Inner object relative to an Outer
                Outer2.Inner inner = Inner();

#< Inner size=1 localVar=1 finalLocalVar=6 >

sonar + findbug + checkstyle +….

this code review tool has cost me much time, due to the poor documentation of sonar, and actually wrong documentaion.

1. the command should be “mvn sonar3:sonar” instead of “mvn sonar:sonar”. the latter would download sonar-maven-plugin-2.0-beta, which would always display the class missing/ class not accessible through classloader exception. the former command would instead download and install sonar-maven-plugin-2.4.1, which is the latest version, using which, the can be utilized locating the needed library jars.
2.there are some checkstyle, pmd null pointer exceptions, this is due to the configuration of each. something like single regexp setting, even though its default value is “^$”, however, specify this explicitly or not, makes a null pointer exception difference. which again proves, sth wrong with the sonar, or the plugin.

3. there are plugins to generate pdf report, sonar-pdfreport-plugin-1.2.jar

4. there are plugins to analyze the jsp, html files, sonar-web-plugin-1.0.2.jar. (however, Please note you can run sonar analysis for an artefact in only one language (SONAR-926). So you cannot run a web analysis and a java analysis on the same maven project. The fix is to make a separate maven pom for the web project with a different name. )

very good articles on Java Memory


“2. Memory

Java handles its memory in two areas. The heap and the stack. We will start with a short overview of memory in general on a computer. Then the Java heap and stack is explained.
2.1. Native Memory

Native memory is the memory which is available to a process, e.g. the Java process. Native memory is controlled by the operating system (OS) and based on physical memory and other physical devices, e.g. disks, flash memory, etc.

The processor (CPU) of the computer computes the instructions to execute and stores its computation results into registers. These registers are fast memory elements which stores the result of the CPU. The processor can access the normal memory over the memory bus. A amount of memory a CPU can access is based on the size of the physical address which the CPU uses to identify physical memory. A 16-bit address can access 2^16 (=65.536) memory locations. A 32-bit address can access 2^32 (=4.294.967.296) memory locations. If each memory area consists of 8 bytes then a 16-bit system can access 64KB of memory and the 32-bit system can access 4GB of memory.

An OS normally uses virtual memory to map the physical memory to memory which each process can see. The OS assigns then memory to each process in a virtual memory space for this process and maps access to this virtual memory to the real physical memory.

Current 32-bit systems uses an extension (Physical Address Extension (PAE)) which extends the physical space to 36-bits of the operation system. This allows the OS to access 64GB. The OS uses then virtual memory to allow the individual process 4 GB of memory. Even with PAE enabled a process can not access more then 4 GB of memory.

Of course with a 64-bit OS this 4GB limitation does not exists any more.
2.2. Memory in Java

Java manages the memory for use. New objects created and placed in the heap. Once your application have no reference anymore to an objects the Java garbage collector is allowed to delete this object and remove the memory so that your application can use this memory again.
2.3. Java Heap

In the heap the Java Virtual Machine (JVM) stores all objects created by the Java application, e.g. by using the “new” operator. The Java garbage collector (gc) can logically separate the heap into different areas, so that the gc can faster identify objects which can get removed

The memory for new objects is allocated on the heap at run time. Instance variables live inside the object in which they are declared.
2.4. Java Stack

Stack is where the method invocations and the the local variables are stored. If a method is called then its stack frame is put onto the top of the call stack. The stack frame holds the state of the method including which line of code is executing and the values of all local variables. The method at the top of the stack is always the current running method for that stack. Threads have their own call stack.
2.5. Escape Analysis

As said earlier in Java objects are created in the heap. The programming language does not offer the possibility to let the programmer decide if an objects should be generated in the stack. But in certain cases it would be desirable to allocate an object on the stack, as the memory allocation on the stack is cheaper then the memory allocation in the heap, deallocation on the stack is free and the stack is efficiently managed by the runtime.

The JVM uses therefore internally escape analysis to check if an object is used only with a thread or method. If the JVM identify this it may decide to create the object on the stack, increasing performance of the Java program.

& from about GC, from Oracle ,


& from


What Is Stack?

Each Java virtual machine thread has a private Java virtual machine stack, created at the same time as the thread. A Java virtual machine stack stores frames. It holds local variables and partial results, and plays a part in method invocation and return. Because the Java virtual machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java virtual machine stack does not need to be contiguous.

The Java virtual machine specification permits Java virtual machine stacks either to be of a fixed size or to dynamically expand and contract as required by the computation. If the Java virtual machine stacks are of a fixed size, the size of each Java virtual machine stack may be chosen independently when that stack is created. A Java virtual machine implementation may provide the programmer or the user control over the initial size of Java virtual machine stacks, as well as, in the case of dynamically expanding or contracting Java virtual machine stacks, control over the maximum and minimum sizes.

The following exceptional conditions are associated with Java virtual machine stacks:

  • If the computation in a thread requires a larger Java virtual machine stack than is permitted, the Java virtual machine throws a StackOverflowError.
  • If Java virtual machine stacks can be dynamically expanded, and expansion is attempted but insufficient memory can be made available to effect the expansion, or if insufficient memory can be made available to create the initial Java virtual machine stack for a new thread, the Java virtual machine throws an OutOfMemoryError

What Is Heap?

The Java virtual machine has a heap that is shared among all Java virtual machine threads. The heap is the runtime data area from which memory for all class instances and arrays is allocated.

The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); objects are never explicitly deallocated. The Java virtual machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implementor’s system requirements. The heap may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger heap becomes unnecessary. The memory for the heap does not need to be contiguous.

The heap mainly store objects create using or class level variables.

The following exceptional condition is associated with the heap:

  • If a computation requires more heap than can be made available by the automatic storage management system, the Java virtual machine throws an OutOfMemoryError
  • </em>


Incorrect lazy initialization of static field

utilizing sonar, or findbug, report above error message for this code.

public static void updateQuotation(String symbol, RawQuotation newValue) {

if (quotations == null) {
quotations = new HashMap();

quotations.put(symbol, newValue);

quotations is a static variable.

what findbug suggesting is actually about thread safety.

Incorrect lazy initialization and update of static field
This method contains an unsynchronized lazy initialization of a static field. After the field is set, the object stored into that location is further accessed. The setting of the field is visible to other threads as soon as it is set. If the futher accesses in the method that set the field serve to initialize the object, then you have a very serious multithreading bug, unless something else prevents any other thread from accessing the stored object until it is fully initialized.

set up sonar yesterday, then start running against the project

there is one critical error, claiming
Security – Array is stored directly : The user-supplied array ‘lSelectedProdList’ is stored directly.

I just went through a research, then quickly refreshed what I have been done for passing SCJP.

shallow copy of the clone, is to clone the object, with primitive variables copies, reference variables, the values of the reference variable, which is the address of the being referred object copied.

Deep copy clone, is to have another copy of the object, the reference variable referred.


Java provides a mechanism for creating copies of objects called cloning. There are two ways to make a copy of an object called shallow copy and deep copy.
Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, just the references are copied. Thus, if the object you are copying contains references to yet other objects, a shallow copy refers to the same subobjects.
Deep copy is a complete duplicate copy of an object. If an object has references to other objects, complete new copies of those objects are also made. A deep copy generates a copy not only of the primitive values of the original object, but copies of all subobjects as well, all the way to the bottom. If you need a true, complete copy of the original object, then you will need to implement a full deep copy for the object.
Java supports shallow and deep copy with the Cloneable interface to create copies of objects. To make a clone of a Java object, you declare that an object implements Cloneable, and then provide an override of the clone method of the standard Java Object base class. Implementing Cloneable tells the java compiler that your object is Cloneable. The cloning is actually done by the clone method.”

good explanations on inner classes, especially anonymous class

We’ve already seen examples of the syntax for defining and instantiating an anonymous class. We can express that syntax more formally as:

new class-name ( [ argument-list ] ) { class-body }

new interface-name () { class-body }


and Syntax for anonymous classes
Before getting into actual code in the sample program, I want to explain the syntax used to define and instantiate an anonymous class.
The definition and instantiation of an anonymous class uses one or the other of the two expressions shown in Figure 1.
new className(optional argument list){classBody}

new interfaceName(){classBody}
Figure 1

Usually, this expression is included inside a larger overall expression, such as an argument to a method call.

What does the first expression mean?

Here is how I usually explain this syntax to my students. The first expression in Figure 1 starts out fairly normal, but becomes cryptic very quickly. This expression instantiates a new object from an unnamed and previously undefined class, which automatically extends the class named className, and which cannot explicitly implement any interfaces. The body of the new class is given by classBody.

The result of executing this expression is that a new class that extends className is defined, a new object of the new class is instantiated, and the expression is replaced by a reference to the new object.


Spring AOP

the project I am currently working on, has used Spring AOPs, for service auditing purposes.

here is the very detailed explanation on this topic, however, it’s too much detailed, just quickly go through and pick up the important points.

some points to note are,
1. Spring AOP support both schema-based, and aspectJ-based AOP
2. the former is by configurations purely in schema, as defining the aspect, join-point and advice, using XML.
3. the latter, AspectJ-based, is using AspectJ annotations., plus just enable aspectj-autoproxing in schema
4. to Note, choose either or, not both. which is the current situation my project having now, redundant

AOP + OOP, quite good pattern

very good explanation on java imports

import FAQ

1. Q: Does importing all classes in a package make my object file (.class or .jar) larger?

A: No, import only tells the compiler where to look for symbols.
2. Q: Is it less efficient to import all classes than only the classes I need?

A: No. The search for names is very efficient so there is no effective difference.
3. Q: Doesn’t it provide better documentation to import each class explicitly?

A: This shows good intentions, but …
* It’s hard to remember to remove classes when they are no longer used, so the import list is surprisingly often wrong. It can seriously slow down reading because unusual or unexpected class imports make me look for that class, only to discover that it must have been used in an earlier version.
* Explicit class imports permit accidentally defining classes with names that conflict with the standard library names. This is very bad. Using “*” to import all classes prevents this dangerous naming accident.
* It’s annoying to always update this list, altho if you use NetBeans, fixing the list is only a click away (see below).
4. Q: I’ve imported java.awt.*, why do I also need java.awt.event.*?

A: The wildcard “*” only makes the classes in this package visible, not any of the subpackages.
5. Q: Why don’t I need an import to use String, System, etc?

A: All classes in the java.lang package are visible without an import.
6. Q: Is the order of the imports important?

A: No. Group them for readability.


struts 2 action flows

and “All objects in this architecture (Actions, Results, Interceptors, and so forth) are created by an ObjectFactory. This ObjectFactory is pluggable. We can provide our own ObjectFactory for any reason that requires knowing when objects in the framework are created. A popular ObjectFactory implementation uses Spring as provided by the Spring Plugin.”

Besides, for dependency injection, it’s generally referred as, “instead of creating its own required objects, object creation would be done by an object factory.”

For struts, spring integration, using struts-spring plugin, the struts.objectFactory is configured to use the spring object factory as in the struts-plugin.xml. nothing needs to be configured( unless special requirements), other than simply put the struts-spring plugin jar in class path.

for this integration, or DI, or IoC, whatever called, possible usage examples are,


&lt;action name=”check” class=”checkActionClass” ….&gt;


Spring applicationContext.xml

&lt;bean id=”checkActionClass”…..&gt;


which would wire the object, initialized using spring object factory, or framework(till now, i m still not that clear about those concepts, many more books to read).


&lt;action name=”check” class=”whatever(either wired through struts internal auto-wiring, or spring framework, depends on whether its been defined in spring bean definitions, since that’s the priotiry)”/&gt;

while inside this “whateve” action class, its constructors would be initialized first, then followingly, all setter methods conducted!! again, priority goes to spring first.

for example, if there is one property inside the action class, like

class WhateverActionClass{


InterfaceServiceA serviceA;

//methods, using the serviceA method update DB

//setters, set the sevriceA


spring, application-context.xml

&lt;bean id=”serviceA” …./&gt;

after initilization of the whateverActionClass, all its properties setters methods would be called, and set using the bean definitions which has bean id same as its property name; due to the default auto-wiring is byName.


add on, to this DI, or IoC,

java class file version

yesterday trying to build the current project, in the course, having encountered the class file version problem, like saying “class file version 50 required, yet it’s currently version 49” something like this.

This problem is due to the jdk version difference. The currently compiling class is using a jdk version different from its dependency class, which has been already compiled by a different version jdk.

basically, class version 50, is compiled using jdk 6. version 49 using jdk 5. version 48 using jdk 4