Monday, December 31, 2012

Inner classes and static nested classes

Terminology
Nested classes are divided into two categories:
  • static. They are simply called static nested classes.
  • non-static. They are called inner classes. 
Ideology
Reasons for using nested classes:
  • It is a way of logically grouping classes that are only used in one place.
  • It increases encapsulation.
  • Nested classes can lead to more readable and maintainable code.

Notes
  1. Access modifiers (private/public/protected) have no effect on inner classes - independant of the modifier, the classes are public.
  2. Inner classes could access members of outer class, no meter of their access modifier.
Usage out of the outer class

Sunday, December 30, 2012

Passing method arguments - by value or by reference. Theory and examples

When Java passes method arguments (parameters) by value and when by reference?
Here we put the major cases and below you will get an working example of it.

Rules:
  • primitive types are passed by value (example in swapPrimitives function);
  • arrays data is passed by reference (example in swapInArray function);
  • reassigning the argument inside the method does not affect the variable in the caller function;
  • all methods called on the arguments are called on the real object (argument variable could be changed);
We will describe the rules with swap function. Some of the methods are wrong and do not swap its arguments. After the code is the output of the program.

Code example:

Thursday, December 20, 2012

Java tests

Question 1: You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use:
  1. ArrayList
  2. LinkedList
   
Question 2: Inner classes can be defined within methods.

SQL tests

Example data 1:
==============================
|FirstName      |LastName    |
==============================
|'Peter'        |'Hansen'    |
|'Peter'        |'Jackson'   |
|'John'         |'Nilsen'    |
|'David'        |'Jackson'   |
==============================


Question 1: Lets have Example data 1;
How much rows will return this query:

SELECT * FROM Persons A, Persons B WHERE
(A.FirstName = B.FirstName Or  A.LastName = B.LastName) AND NOT
(A.FirstName = B.FirstName AND A.LastName = B.LastName)

Thursday, December 13, 2012

SQL GROUP BY and HAVING in examples

Here we show you the usage HAVING and GROUP BY. As an advance, we have an example with GROUP BY multiple columns.
In first section you have got the data schema. Later you have some tasks on it. After that you have the solutions for the tasks.

Singleton design pattern in examples

The singleton is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.

There are different ways of implementation.

Lazy Singleton aka Singleton with double-checked locking

Wednesday, December 12, 2012

Kickstarting WebServices with REST

Here is an another nice tutorial for web services in Java. This time, the protocol used is REST, so we get RESTful services. There is a how-to to create server application for webservice. The build system is Maven.

TODO - to be tested!

hashCode() and equals()

Rule 1: The connection between hashCode() and equals():
If x.equals(y), then x.hashCode() must be same as y.hashCode().

Rule 2: Overwriting hashCode() or equals():
If you overwrite one of these functions, you have to overwrite the second too.

When you need to overwrite equals?

Tuesday, December 11, 2012

Stateless and stateful protocols

A stateless protocol is a communications protocol that treats each request as an independent transaction that is unrelated to any previous request so that the communication consists of independent pairs of requests and responses. A stateless protocol does not require the server to retain session information or status about each communications partner for the duration of multiple requests. In contrast, a protocol which requires the keeping of internal state is known as a stateful protocol.


Examples of statefull and stateless protocols

Saturday, December 8, 2012

Friday, December 7, 2012

having syntax in SQL

sql HAVING example:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000



sql HAVING Syntax:
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value



Here is a nice example with having.

Could we have memory leaks in Java?

Yes, here it is explained why.

Unused references

Code that keep useless references to objects that will never use again, and at the same time never drop those references; one may say those cases are not "true" memory leaks because there are still references to those objects around, but if the program never use those references again and also never drop them, it is completely equivalent to (and as bad as) a "true memory leak".

Java Native Interface

While implementing JNI functionality, you could

Tuesday, December 4, 2012

Defining dependency projects in Maven

The problem: You have 2 projects and one relies on the second. How to realize that with maven?

Solution: Make the inner one. And define it as dependency in the another. Use the same groupId and artifactId. Easy one!

But if project Z depends on projects A and project B. And A and B use different versions of a library. Which version will be used in calling project?

Here is the code of this situation:

Differences between ear, jar and war files

Lets see the similarities and differences between ear, jar and war files.
All these files are simply zipped files using java jar tool, but they are created for different purposes.

JAR files
The .jar files contain the libraries, resources and accessories files like property files. JAR file could have an optional Manifest file, which contains package and extension related data.