Home > Java > JavaInterview questions > Java reflection common interview questions

Java reflection common interview questions

(*-*)浩
Release: 2019-12-28 15:58:07
Original
3824 people have browsed it

Java reflection common interview questions

What is reflection?

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

Java reflection:                                                  (Recommended study : java common interview questions)

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?

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.

Judge the member variables and methods of any class at runtime.

Call the method of any object at runtime.

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.

Under what circumstances is serialization required:

a) When you want to save the object state in memory to a file or database;

b) When you want to use sockets to transfer objects over the network;

c) When you want to transfer objects through RMI;

Dynamic proxy is What? 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 new classes for 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 agent:

Spring’s AOP

Add transaction

Add permissions

Add log

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, but in fact it is just a tool class that generates proxy objects).

Use InvocationHandler to splice the proxy class source code, 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.

For more Java related technologies, please visit the java video tutorial column to learn!

The above is the detailed content of Java reflection common interview questions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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