Java - Write code to Return the name part of the given qualified java class name.

Requirements

Write code to Return the name part of the given qualified java class name.

Demo

/*
 * Copyright (c) 2001-2002, Marco Hunsicker. All rights reserved.
 *
 * This software is distributable under the BSD license. See the terms of the
 * BSD license in the documentation provided with this software.
 *///  www .  ja  va 2  s  .  c o  m
//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String qualifiedName = "abc.book2s.com.def";
        System.out.println(getClassName(qualifiedName));
    }

    /**
     * Returns the name part of the given qualified java class name.
     *
     * @param qualifiedName name for which the name part should be returned.
     *
     * @return name part of the given name. Returns <code>null</code> if the given name
     *         was <code>null</code> itself.
     *
     * @see #getPackageName
     */
    public static String getClassName(String qualifiedName) {
        int lastDot = qualifiedName.lastIndexOf('.');

        if (lastDot > 0) {
            return qualifiedName.substring(lastDot + 1);
        }
        return qualifiedName;
    }
}