Java this class getname

How to get the name of a class without the package?

In C# we have Type.FullName and Type.Name for getting the name of a type (class in this case) with or without the namespace (package in java-world). What is the java equivalent to Type.Name ? Clearly there must be a better way than using Class.getName() and strip it of the package name manually.

I did a quick search on this and was not to impressed by the first results that I found on Google so consider this as a freebie.

Forget Google! The first place to look for answers about the Java standard class library are the Java API docs (aka javadocs): java.sun.com/javase/reference/api.jsp

2 Answers 2

Returns the simple name of the underlying class as given in the source code. Returns an empty string if the underlying class is anonymous.

The simple name of an array is the simple name of the component type with «[]» appended. In particular the simple name of an array whose component type is anonymous is «[]».

It is actually stripping the package information from the name, but this is hidden from you.

It also maps the internal name of an anonymous class to an empty String and does stuff with array class names.

I didn’t know it could return an empty string, and IMHO that’s a design flaw. If there is no simple name it should throw an exception.

Читайте также:  Javascript count arrays in array

For inner classes, it strips not only the package but also the outer class’ name. (I wanted the other behaviour, so this answer is wrong for me.)

@toolforger Would this.getClass.getEnclosingClass.getSimpleName + «$» + this.getClass.getSimpleName work for your purpose?

@AlonsodelArte Not sure (there are complications like multiple nesting levels and anonymous classes). Not too relevant anyway, I just wanted to point out that the answer is technically incorrect for the question as written.

Источник

What does Class.getName() do for a given class?

Suppose i have an class «employee» having an object obj. then how the obj.getClass().getName() statement will be performed ? And what it should be return ?

It’s not clear to me what you mean by «how will [a statement] be executed?». Are you asking about how the JVM performs dynamic dispatch at runtime? How the compiler generates bytecode? Or simply how to write code that will cause this to be called (though the latter seems trivially straightforward)?

I don’t understand this your question. Is obj an instance of the employee class? Or is it a member variable of an employee? Or something else?

3 Answers 3

The getClass() method gets the actual class of the object, which may be different from the class of the variable holding the object. The getName() method will return the full package plus class name of that class as a string, like this:

For example, the following code outputs the above String:

package com.company.project.package; class MyClass extends Object < // some definition >Object o = new MyClass(); System.out.println(o.getClass().getName());

Disclaimer: While Erick’s answer is correct, its not full.

This method is similar to getCanonicalName() with a difference — it returns binary name of the class, where canonical version returns null (e.g. for anonymous classes).

If this class object represents a reference type that is not an array type then the binary name of the class is returned:

String.class.getName() returns "java.lang.String" (new Object() <>).getClass().getName() returns "test.Main$1" 

If this class object represents a primitive type or void, then the name returned is a String equal to the Java language keyword corresponding to the primitive type or void.

byte.class.getName() returns "byte" long.class.getName() returns "long" 

If this class object represents a class of arrays, then the internal form of the name consists of the name of the element type preceded by one or more ‘[‘ characters representing the depth of the array nesting:

(new Object[3]).getClass().getName() returns "[Ljava.lang.Object;" (new int[3][4][5][6][7][8][9]).getClass().getName() returns "[[[[[[[I" 

The encoding of element type names is as follows:

Element Type Encoding boolean Z byte B char C class or interface L; double D float F int I long J short S 

Источник

getting only name of the class Class.getName()

Java has great online documentation. The page that andyb linked below is an example. Any time you have a question regarding the exact functionality available from a class or package, the javadocs are as easy to find as Googling «java 6 «.

7 Answers 7

Thank you, @andyb getSimpleName() is easier to access class name minus the package name. Syntax : object.getClass().getSimpleName() Sample code

Does not work for inner classes: This will strip out the enclosing class’ name, not just the package.

The below both ways works fine.

System.out.println("The Class Name is: " + this.getClass().getName()); System.out.println("The simple Class Name is: " + this.getClass().getSimpleName()); 

The Class Name is: package.Student

The simple Class Name is: Student

String s = String.class.getName(); s = s.substring(s.lastIndexOf('.') + 1); 

@eckes if the dot isn’t found, the result will be -1, adding 1 will give you a substring starting at index 0 — exactly what you need in this case. (I just tested that with int[] , the result is «[I» .)

You can use following simple technique for print log with class name.

private String TAG = MainActivity.class.getSimpleName(); 

Suppose we have to check coming variable value in method then we can use log like bellow :

private void printVariable()

Importance of this line is that, we can check method name along with class name. To write this type of log.

write :- loge and Enter.

E/MainActivity: printVariable: 

Источник

Оцените статью