search
HomeJavaJavaInterview questionsJava reflection common interview questions

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools