2

I keep coming across this syntax and I have been unable to find any explanation for it.

private static final String TAG = MainActivity.class.getSimpleName();
Log.i(TAG,”some log statement”);

What does the class keyword signify in MainActivity.class.getSimpleName();

A full picture of how this code could look and be used

public class MainActivity extends AppCompatActivity {
    // What does "class" signify in the following
    // MainActivity.class.getSimpleName();
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "onCreate()");
    }
}
hata
  • 11,633
  • 6
  • 46
  • 69
Tim
  • 1,606
  • 2
  • 21
  • 42

5 Answers5

8

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

Just gets the "simple name" of the class MainActivity, which happens to be "MainActivity". One could just write

private static final String TAG = "MainActivity";

as well, but if you later decide to rename the class, you'll have to take care to also change that string constant because no compiler will tell you if you forget it.

Note that SomeClass.class gives you an object which describes the class itself, see https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html.

As such, Someclass.class.getSimpleName() is rarely useful except for logging. However, if you have an object, you can find information about its type by calling someObject.getClass(). This gives you the Class of the object which you can then use to query about the properties of the object's type, e.g. which methods are declared or what the full class name (getCanonicalName()) is.

In Java, this technique is called "reflection". Read more about it e.g. here, or here.

Maybe also note that, as a 'normal' programmer, you don't need to know much about reflection beyond the instanceof operator. However, some frameworks depend heavily on reflection. Java Persistency API (JPA) providers, for instance, or dependency injection (DI) frameworks; actually, most tools you control by putting annotations in your code need reflection to find and handle them at runtime.

Community
  • 1
  • 1
JimmyB
  • 12,101
  • 2
  • 28
  • 44
2

What is the class keyword in MainActivity.class.getSimpleName();

MainActivity.class is the syntax for getting the Class object representing the identified Java class.

Does this create an inline method called getSimpleName(); that is added to the MainActivity?

No. getSimpleName() is a method on Class.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
2

The .class Syntax

If the type is available but there is no instance then it is possible to obtain a Class by appending ".class" to the name of the type. This is also the easiest way to obtain the Class for a primitive type.

Since the MainActivity type is available, but there is no instance, you can get MainActivity for a primitive type by appending ".class". Here is the link: Retrieving Class Objects

boolean b;
Class c = b.getClass();   // compile-time error

Class c = boolean.class;  // correct

Then you can call getSimpleName() to return a string signifying the name of the underlying class. In your case, "MainActivity".

This is preferable to just hard coding "MainActivity" because if you refactor your code and change the classname, you don't have to change the value of TAG, which is used for logging and debugging.

kimbaudi
  • 13,655
  • 9
  • 62
  • 74
1

Every class loaded by the JVM has a class object. The class object is effectively a singleton and it holds all the static fields and methods of that class. You can refer to it using the syntax you posted, ClassName.class.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
1

The java.lang.Class.getSimpleName() returns the simple name of the underlying class as given in the source code. Returns an empty string if the underlying class is anonymous.