@Transient annotation vs transient keyword in Java

For a Java beginner it can be a bit confusing to see that the Java language has both the keyword transient, and that JPA (Java Persistence API) has an annotation named @Transient, and that they seem to mean different things (which they infact do).

The transient keyword tells the JVM to not serialize a specific attribute in an object. To serialize an object means for example to write it to a file, send it over the network etc.

The @Transient annotation in JPA tells the JVM to not persist the attribute in the database when the object is saved.

So why not use only the keyword for both cases? You could have an object where you don’t want to save the attribute to the database, but you want to serialize it when you write it to file for example. Or vise versa, you want to save the attribute to the database but not serialize it when you write it to a file.

How to deal with “java.lang.OutOfMemoryError: Java heap space” error

When you encounter this type of error, it means that you have run out of memory in the heap while running your Java application.

There are many different reasons to why this could happen, and if you feel the default size of the heap should be enough then you’d probably should take a good look at the code and see if things can be done differently. Otherwise you could set the max heap size through a parameter to the JVM.

In your IDE, locate the place where VM arguments is set and add:

-Xmx2048m

to set the max heap size. The example above sets it to about 2Gb.

Difference between Java SE, Java EE, Java ME and java FX

There are four Java platforms:

    Java Platform, Standard Edition (Java SE)
    Java Platform, Enterprise Edition (Java EE)
    Java Platform, Micro Edition (Java ME)
    JavaFX

Below are brief explanations of each of the four different Java platforms:

Java SE is for developing desktop applications and it is the foundation for developing in Java language. It consists of development tools, deployment technologies, and other class libraries and toolkits used in Java applications. Java EE is built on top of Java SE, and it is used for developing web applications and large-scale enterprise applications. Java ME is a subset of the Java SE. It provides an API and a small-footprint virtual machine for running Java applications on small devices. JavaFX is a platform for creating rich internet applications using a lightweight user-interface API. It is a recent addition to the family of Java platforms.

Strictly speaking, these platforms are specifications; they are norms, not software. The Java Platform, Standard Edition Development Kit (JDK) is an official implementation of the Java SE specification, provided by Oracle. There are also other implementations, like OpenJDK and IBM’s J9.

How to Create a Singleton Object

This example shows how to create a singleton object. A singleton is a class of which there can only be one instance in the same Java Virtual Machine.
To create a singleton there has to be a private constructor because the class will itself control the one and only instance that will be created, and of course a private constructor cannot be called from outside the class.
Instead, a method is created with public access that returns the singleton instance (if the method is called the first time the object is instantiated). The example class MySingleton illustrates this:

Continue reading