Java Access Level for Members: public, protected, private

Java access level contains two parts: 1) access level for classes and 2) access level for members. For class access level, the keyword can be public or no explicit modifier(package-private). For member access level, the keyword can be public, protected, package-private (no explicit modifier), or private. The following table summarizes the access level of different … Read more

How Java Compiler Generate Code for Overloaded and Overridden Methods?

Here is a simple Java example showing Polymorphism: overloading and overriding. Polymorphism means that functions assume different forms at different times. In case of compile time it is called function overloading. Overloading allows related methods to be accessed by use of a common name. It is sometimes called ad hoc polymorphism, as opposed to the … Read more

What is Instance Initializer in Java?

In this post, I will illustrate what are instance variable initializer, instance initializer, static initializer, and how the instance initializer works in Java. 1. Execution Order Look at the following class, do you know which one gets executed first? public class Foo {   //instance variable initializer String s = "abc";   //constructor public Foo() … Read more

Yet Another “Java Passes By Reference or By Value”?

This is a classic interview question which confuses novice Java developers. In this post I will use an example and some diagram to demonstrate that: Java is pass-by-value. 1. Some Definitions Pass by value: make a copy in memory of the actual parameter’s value that is passed in. Pass by reference: pass a copy of … Read more

Java equals() and hashCode() Contract

The Java super class java.lang.Object defines two important methods: public boolean equals(Object obj) public int hashCode()public boolean equals(Object obj) public int hashCode() In this post, I will first show an example of a common mistake, and then explain how the equals() and hashCode() contract works. 1. A common mistake The common mistake is shown in … Read more

How to Build Your Own Java library?

Code reuse is one of the most important factors in software development. It is a VERY good idea to put frequently-used functions together and build a library for yourself. Whenever some method is used, just simply make a method invocation. For Java, it’s straightforward to manage such a library. Here a simple example in Eclipse. The library will contain only one “add” method for demo purpose.

Step 1: Create a “Java Project” named as “MyMath”, and a simple “add” method under “Simple” class.

Package structure is as follows:

Read more

Java method for spliting a camelcase string

This Java method accepts a camel case string, and returns a linked list of splitted strings. It has been designed to handle various kinds of different cases and fully tested and good to use. Handling camel case in Java involves a lot of styles, this methods can handle the following cases: Regular camel case names, … Read more