Java Example: Reversing a string using StringBuilder Class

This entry is part 54 of 54 in the series Learn Java

Java provides us with a StringBuilder class that allows us to reverse a string with only one statement.

Consider the following example.

package reverse;

public class Reverse {

    private static String reverseString(String str) {

        return new StringBuilder(str).reverse().toString();


    public static void main(String[] args) {

        String input = "Hotel California";




Java: Final method

This entry is part 53 of 54 in the series Learn Java

As was mentioned in the previous post, we can define a method as a final method so that it can not be overriden in the subclasses.

package overridetest;

class Music {

    int i = 1;

    final int fan() {
        return i;

    static String groupie() {
        return "Music";

class Rock extends Music {

    int i = 2;

    static String groupie() {
        return "Rock";

public class OverrideTest {

    public static void main(String args[]) {
        Rock r = new Rock();



If you define a class as a final class, you will not be able to extend it.  Extending a final class will create a compile time error.

Java: The final keyword

This entry is part 52 of 54 in the series Learn Java

The final keyword in Java is very similar to const in C++.  It can be used to apply to variable, method or a class in Java.

If it is applied to variable, it means the variable could not later be changed.

If it is applied to method, it means the method cannot not be overriden by subclasses.

If it is applied to object, it means the methods in the object is final.

In the example below, we apply the final keyword to a variable.

package music;

public class Music {

    public static class Play {

        final int count = 10;

        void play() {


    public static void main(String[] args) {

        Play p = new Play();;