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

Video Face Swap

Video Face Swap

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

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment