Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

 

and many more ....

 

 

Exceptions in Constructors

In this example we shall show you how to handle exceptions in constructors. To handle exceptions in constructors we have performed the following steps: We have created a class, InputFile, that has a BufferedReader field. In its constructor, it gets a String and it creates a new FileReader with the given String name of path to file to read from. A FileNotFoundException might ...

Read More »

Conflicting Constructors

With this example we are going to demonstrate how to use conflicting constructors in a class. In short, to use conflicting constructors in a class we have followed the steps below: We have created a class ConflictingConstructors, that has a constructor without fields and a constructor that gets an int value and throws an IllegalArgumentException if it is smaller than ...

Read More »

Generic deep copy

This is an example of how to make a generic copy of a class, using Java reflection. In order to make a generic copy of a class we have created two classes and then copied the one to the other, as described below: We have created two classes A and B. B class has a String and an int field ...

Read More »

Avoid side-effects with Object.clone

In this example we shall show you how to avoid side-effects when using Object.clone. To avoid side-effects when using Object.clone we have performed the following steps: We have created a class, Enemy, that has a method, void mungeFunc(SideEffects sdf), where it gets an object of SideEffects class and changes its fields’ values. SideEffects class implements the Cloneable interface to indicate to the ...

Read More »

Inheritance constructor calls example

With this example we are going to demonstrate how to inherit constructor calls from a super class to a sub class. In short, to inherit constructor calls from a super class to a sub class: We have created a class A, with a constructor and a class B that extends A and also has a constructor. We have also created ...

Read More »

Initialize constructor with composition

This is an example of how to initialize a constructor with composition. We have created an object with reference to another object, as described below: We have created class A that has a String field and overrides the toString() API method of Object, where it returns its String field. We have also created a class, Composition that has four String fields, ...

Read More »

Default constructor calls superclass constructor

In this example we shall show you how to call the superclass constructor in a default class constructor. An easy way to call a superclass constructor in a class constructor is to create a class that extends another class, as shown below: We have created a class A that has a default constructor without fields. We have also created class ...

Read More »

Use serializable interface to perform deep copy

With this example we are going to demonstrate how to use the Serializable interface to perform a deep copy of a class. Classes implement the Serializable interface in order to have their state serialized or deserialized. In short, to use the Serializable interface to perform a deep copy of a class: We have created a class, Car that implements the ...

Read More »

Deep Copy example – Part 2

This is an example of how to create a deep copy of a class. In order to create a deep copy of a class we have overriden the clone() API method of Cloneable interface, as described in the following steps: We have created a class, Car that has a String field and a getter and setter method for it. It also ...

Read More »

Shallow Copy example

In this example we shall show you how to create a shallow copy of a class. To create a shallow copy of a class we have performed the following steps: We have created a class, Car that has a String field and a getter and setter method for it. It also has a constructor using its String field. We have also created ...

Read More »

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns