search
HomeJavaJavaInterview questionsJava internal classes and reflection interview questions

Java internal classes and reflection interview questions

#1. What is the difference between static nested class and inner class?

Static nested class: Static Nested Class is an inner class declared as static (static), which can be instantiated without relying on external class instances.

Inner class: It needs to be instantiated after the external class is instantiated before it can be instantiated. Its syntax looks quite weird. (Recommended study: java interview questions)

2. Where in the following code will compilation errors occur?

class Outer {
    class Inner {
    }
    public static void foo() {
        new Inner();
    }
    public void bar() {
        new Inner();
    }
    public static void main(String[] args) {
        new Inner();
    }
}

Note: The creation of non-static inner class objects in Java depends on its outer class objects. In the above interview questions, the foo and main methods are both static methods. There is no this in the static method, that is It says that there is no so-called outer class object, so inner class objects cannot be created. If you want to create an inner class object in a static method, you can do this:

new Outer().new Inner();

Reflection in Java

Tell me about your understanding of reflection in Java

Reflection in Java is first able to obtain the bytecode of the reflection class in Java. There are three ways to obtain the bytecode:

Class.forName(className).

Class name.class.

this.getClass().

Then the methods, variables, constructors, etc. in the bytecode are mapped into corresponding Method, Field, Constructor and other classes. These classes provide a wealth of methods that can be used by us.

The above is the detailed content of Java internal classes and reflection 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)