Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the getClass () and getName () methods in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "getClass () and how to use the getName () method in Java". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The getClass () and getName () methods use getClasspublic final Class getClass ()

Returns the runtime class for this Object. The returned Class object is locked by the static synchronized method of the represented class.

Java's reference variables have two types, the compile-time type and the run-time type. The compile-time type is determined by the type used when the variable is declared, and the run-time type is determined by the object actually assigned to the variable.

Public class StudentDemo {public static void main (String [] args) {Student ss = new StudentSon (1,2); Class a = ss.getClass (); System.out.println (a.getName ());}} getNamepublic String getName ()

Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object as String.

Entities: destination referenced by the object

If the runtime type of this class is StudentSon and the compile-time type is Student, ss.getClass () returns the StudentSon class.

Running result:

StudentSon

About getClass () and getName () A question package test;import java.util.Date; public class SuperTest extends Date {private static final long serialVersionUID = 1L; private void test () {System.out.println (super.getClass (). GetName ());} public static void main (String [] args) {new SuperTest (). Test ();}}

1.super.getClass () is the getClass () method of the parent class, whose parent class is Date, whose getClass () method inherits from the Object class and is not overridden

two。 When calling the getName () method, and getName () is: package name + class name

So the output is

Test.SuperTest

This is the end of the introduction to "getClass () and how to use the getName () method in Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report