Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 695
rkgarg Avatar asked Mar 21 '11 17:03

rkgarg


People also ask

What does getName () do in Java?

The getName() method is a part of File class. This function returns the Name of the given file object. The function returns a string object which contains the Name of the given file object. If the abstract path does not contain any name then a null string is returned.

What does class forName () method returns?

forName. Returns the Class object associated with the class or interface with the given string name, using the given class loader. Given the fully qualified name for a class or interface (in the same format returned by getName ) this method attempts to locate, load, and link the class or interface.

What does class <?> Mean in Java?

What Does Class Mean? A class — in the context of Java — is a template used to create objects and to define object data types and methods. Classes are categories, and objects are items within each category. All class objects should have the basic class properties.

How do I fetch a class name?

The simplest way is to call the getClass() method that returns the class's name or interface represented by an object that is not an array. We can also use getSimpleName() or getCanonicalName() , which returns the simple name (as in source code) and canonical name of the underlying class, respectively.


1 Answers

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:

com.company.project.package.MyClass

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());
like image 156
Erick Robertson Avatar answered Oct 27 '22 14:10

Erick Robertson