The play of instantiating java.lang.Void

Effect

This article creates an object of java.lang.Void, This is an article that tried to call the method of the object.

However, creating an object of java.lang.Void probably has no merit, so it's just a play.

code

I also wrote a commentary in the comments.

import java.lang.reflect.Constructor;

public class CreateVoid {
    public static void main(String... args) throws Exception {
        //How to get Class by specifying the class name as a character string
        Class<Void> voidClazz1 = (Class<Void>)Class.forName("java.lang.Void");
        Void o1 = getVoid(voidClazz1);

        //How to get the Class from the actual method
        Class<Void> voidClazz2 = (Class<Void>)CreateVoid.class.getMethod("voidMethod", new Class[]{}).getReturnType();
        Void o2 = getVoid(voidClazz2);

        System.out.println(o1);//java.lang.Void@1c6b6478
        //Since the equals method is not overridden, it will always be false if it is another instance.
        System.out.println(o1.equals(o2));//false
        System.out.println(o1.hashCode());//476800120
        System.out.println(o1.toString());//java.lang.Void@1c6b6478
        System.out.println(o1.getClass());//class java.lang.Void
    }

    //If it is void, it will be a primitive type, so Void(=java.lang.Void)is.
    public Void voidMethod(){return null;};

    //Class<Void>Create a Void object from
    private static Void getVoid(Class<Void> voidClazz) throws Exception {
        //Looking into the JDK source`private Void() {}`And the constructor is private so you need to do the following
        //1.Write getDeclaredConstructor instead of getConstructor
        //2.Set the accessible flag to true
        Constructor<?> declaredConstructor = voidClazz.getDeclaredConstructor(new Class[]{});
        declaredConstructor.setAccessible(true);
        return (Void)declaredConstructor.newInstance(new Object[]{});
    }
}

Recommended Posts

The play of instantiating java.lang.Void
The world of clara-rules (2)
Judgment of the calendar
The world of clara-rules (4)
The world of clara-rules (1)
The world of clara-rules (3)
The world of clara-rules (5)
The idea of quicksort
The idea of jQuery
About the handling of Null
About the description of Docker-compose.yml
Understand the basics of docker
Explanation of the FizzBuzz problem
The basics of Swift's TableView
Median of the three values
The illusion of object orientation
Switch the version of bundler
About the behavior of ruby Hash # ==
[Java] Delete the elements of List
Continuation: The parable of OOP (omitted)
Qualify only part of the text
Understand the basic mechanism of log4j2.xml
'% 02d' What is the percentage of% 2?
[Rails] Check the contents of the object
Replace the contents of the Jar file
Check the version of Cent OS
[Swift] Change the textColor of UIDatePicker
[Ruby] See the essence of ArgumentError
Explanation of the order of rails routes
I read the source of ArrayList I read
The basics of SpringBoot + MyBatis + MySQL
Note on the path of request.getRequestDispatcher
The basic basis of Swift dialogs
The basic basis of Swift's Delegate
I read the source of Integer
This and that of the JDK
Check the migration status of rails
The story of @ViewScoped consuming memory
Filter the fluctuations of raw data
Explaining the columns of Spree :: Taxonomy
A memorandum of the FizzBuzz problem
I read the source of Long
Official logo list of the service
Explain the column of Spree :: Product
Various methods of the String class
About the role of the initialize method
Think about the 7 rules of Optional
Get the ID of automatic numbering
I read the source of Short
I read the source of Byte
Order of processing in the program
I read the source of String
The origin of Java lambda expressions
[Ruby] Display the contents of variables
Image processing: Let's play with the image
Filter the result of BindingResult [Spring]
Summary about the introduction of Device
Remote debugging of the Cognos SDK
About the log level of java.util.logging.Logger
The story of releasing the Android app to the Play Store for the first time.