2020 New Java Interview Questions-Reflection

王林
Release: 2020-06-12 16:26:54
forward
1893 people have browsed it

2020 New Java Interview Questions-Reflection

1. What is reflection?

Reflection mainly refers to the ability of a program to access, detect and modify its own state or behavior

Java reflection:

In the Java runtime environment , for any class, can you know what properties and methods this class has? For any object, can any of its methods be called?

(Recommended related video tutorials: java video tutorial)

The Java reflection mechanism mainly provides the following functions:

  • In Determine the class to which any object belongs at runtime.

  • Construct an object of any class at runtime.

  • Determine the member variables and methods of any class at runtime.

  • Call the method of any object at runtime.

#2. What is java serialization? When is serialization required?

Simply put, it is to save the status of various objects in memory (that is, instance variables, not methods), and to read out the saved object status. Although you can use your own various methods to save object states, Java provides you with a mechanism that should be better than your own to save object states, and that is serialization.

(recommended related tutorials: Getting Started with Java)

Under what circumstances is serialization required:

  • When you want to When the object state in memory is saved to a file or database;

  • When you want to use sockets to transmit objects over the network;

  • When you want to transfer objects through RMI;

3. What is a dynamic proxy? What are the applications?

Dynamic proxy:

When you want to add some additional processing to the methods in the class that implements a certain interface. For example, add logs, add transactions, etc. You can create a proxy for this class, so the name implies that you create a new class. This class not only contains the functions of the original class methods, but also adds a new class with additional processing on the original basis. This proxy class is not defined but is dynamically generated. It has decoupling significance, flexibility and strong scalability.

Application of dynamic proxy:

  • Spring’s AOP

  • Add transaction

  • Add permissions

  • Add log

4. How to implement dynamic proxy?

First you must define an interface, and there must also be an InvocationHandler (pass the object of the class that implements the interface to it) processing class. There is another tool class Proxy (it is customary to call it a proxy class because calling its newInstance() can generate a proxy object. In fact, it is just a tool class that generates proxy objects). Use InvocationHandler to splice the source code of the proxy class, compile it to generate the binary code of the proxy class, load it using the loader, instantiate it to generate the proxy object, and finally return.

Recommended interview questions: java interview questions

The above is the detailed content of 2020 New Java Interview Questions-Reflection. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template