×

Java Class Name

How to write a class name?

The following considerations should be made while writing class names.

  • The name of the current class shouldn't be based on a preset or existing class.
  • Java keywords shouldn't be used as class names.
  • The class name should begin with a capital letter and end with a tiny letter (mixed case).
class Example

Every word in the name should start with a capital letter, and the other characters should be small.

class Main

It is advised to keep interface names short and descriptive.

It is preferable to avoid acronyms when writing class names.

Example

Main.java

public class Main {
   public void Method1 ( ) {
      System . out . println ( " In  Method1 " ) ;   
   }
   public void Method2 ( ) {
      System . out . println ( " In Method 2 " ) ;   
   }
   public static void main ( String args [ ] ) {
      Main o1 = new Main ( ) ;
      o1 . Method1 ( ) ;
      o1 . Method2 ( ) ;
   }
}

Output

This is sample method
This is demo method

There are four methods on the Class API that can be used to get the name of a class: getSimpleName(), getName(), getTypeName(), and getCanonicalName ().

GetSimpleName() Method

There are two categories of names in Java: qualified and simple. A qualified name is a list of simple names separated by dots, whereas a simple name is an individual identifier.

GetSimpleName(), as its name implies, returns the underlying class's simple name, which is the name specified for it in the source code.

package com.baeldung.className;
public class RetrieveClassName {}

RetrieveClassName: would be its short name.

assertEquals("RetrieveClassName",RetrieveClassName.class.getSimpleName())); 

Primitive types and arrays can also be given short names. For primitive types like int, boolean, and float, that will just be their names.

Additionally, the method will return an array's type in its simplest form, followed by opening and closing braces for each dimension ([]):

RetrieveClassName[] n = new RetrieveClassName[];
assertEquals("RetrieveClassName[]", n.getClass().getSimpleName());

Finding Additional Names

We should now examine how we might discover a class's name, type name. These names seek to provide more details about the class than getSimpleName() does.

The canonical name as specified in the Java Language Specification is always returned by the getCanonicalName() function.

In terms of the other ways, the results may vary slightly depending on the use cases. We'll explore what that means for various object and primitive types.

Primitive Types 

Since primitive types are the most basic, let's start with them. The results of the three methods getName(), getTypeName(), and getCanonicalName() for primitive types are identical to those of getSimpleName().

Object Types

We'll see how these techniques function with object types in a moment. They all return the class's canonical name, which is normally how they behave.

Most of the time, this is a qualified name that also includes the class simple name and all of the class package simple names.

Inner Classes

The majority of these method calls behave as we've seen in the previous section, but there are a few outliers.

One of them is inner classes. When used to inner classes, the getName() and getTypeName() methods act differently than the getCanonicalName() method.

The canonical name of the class, which is the canonical name of the enclosing class plus the simple name of the inner class separated by a dot, is still returned by the getCanonicalName() method.

Anonymous Classes

They lack a canonical name as well as a simple name, as we have already shown. Consequently, getCanonicalName() returns nothing. When invoked on an anonymous class, getCanonicalName() will return null rather than an empty string, in contrast to getSimpleName().

The canonical name of the calling class, a dollar amount, and a number denoting the anonymous class's position among all other anonymous classes formed in the calling class are returned by the getName() and getTypeName() functions, respectively.

Arrays

Finally, let's look at how the aforementioned three approaches deal with arrays.Each method will adjust its typical result to reflect the fact that we are working with arrays. The results of the getTypeName() and getCanonicalName() functions will have bracketed pairs appended to them.To distinguish the inner class portion from the remainder of the name, the first call utilises a dollar sign in place of a dot.


Related Topics

Duodecimal in Java

Duodecimal is a notation style in which a number with a base of 12 is referred to be a duodecimal number. In Java, we can use to convert duodecimal integers...

2 minutes read.

What’s New in Java 15

Sealed classes are the new concept that was introduced by Java 15. Sealed classes are a preview feature. Most of the features which are released in java 15 are in...

3 minutes read.

Java Serialization

JAVA SERIALIZATION Serialization is a process by which objects can be represented as a sequence of bytes. These bytes have information about object's data, object's type and datatypes of members in...

3 minutes read.

Functional Interface in Java 8

A brief introduction to Interface in Java: Interfaces in Java are basically the blue print of classes. Before the appearance of Java 8, it was only possible to declare one or...

11 minutes read.

Java Base64 Encoding and Decoding

Introduction to Encoding and Decoding Encoding is the process of putting the sequence of characters like letters, numbers, punctuations, and other symbols into a specialised format for the efficient transmission or...

11 minutes read.

Java Default Keyword

The Default keyword in java programming language is used as access modifier.If any of the variable or the constructor or the methods or the classes are not assigned with the...

3 minutes read.

Prime Number Program in Java

Prime Number Program in Java using for loop A natural number which is greater than 1 and has only two factors the number itself and 1 is called prime number. In...

2 minutes read.

Deque in Java

Deque in java collections with Example Deque is short for “double-ended queue.” It is a linear collection that extends the Queue interface and supports insertion and deletion of the element at both the...

3 minutes read.

Least Operator to Express Number in Java

In this article, we will learn about how to obtain a target number using a single number or a single integer by leveraging least operators in Java. There can be...

3 minutes read.

How to get ASCII value of char in Java

Introduction: On this application, you will learn how to find and show the ASCII value of char in Java. That is done with the use of type-casting and also everyday...

4 minutes read.

Java Binary Operators

In this article, we are going to discuss about the binary operators in Java and their operations. Also, an example program will be discussed along with the operations. These are...

6 minutes read.

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.

Difference between Constructor and Method in Java

What is Constructor? In Constructor, we will discuss constructors and also will discuss default constructors and finally, we will discuss overloading constructors. A constructor is a method that is used to...

13 minutes read.

Java Font

The font is a Java class that is a part of java.awt package. The Serializable interface is implemented by it. The direct recognized child of a Java Font class is...

8 minutes read.

Factorial Program in Java using Recursion

Factorial Program in Java Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to...

3 minutes read.

Sort Dates in Java

The Collection class's sort() method, which is a component of the Comparator mechanism, arranges the data in decreasing order. The Comparator interface can be used to accomplish the goal while...

4 minutes read.

Zygodromes in Java

Zygodrome is a positive number created by the same digits running non-trivially. A number is called a zygodrome if identical digits constantly occur together (in pairs). The Greek word "zyg"...

4 minutes read.

Java Binary to Hexadecimal

Converting between types in programming is an important task. Moving from one kind to another kind conversion is occasionally necessary. We have discussed numerous conversion types in the section on...

3 minutes read.

Java Math abs() Method

The abs() method of Math class returns the absolute value of the argument where the argument can be int, double, float, long. Syntax public static int abs(int a) public static float abs(float a) public...

2 minutes read.

Java List Implementations

ArrayList and LinkedList are the two implementations of List that are for general-purpose. You'll probably utilise an array list the majority of the time because it's quick and provides constant-time...

3 minutes read.