Home  >  Article  >  Java  >  Active use and passive use like Java

Active use and passive use like Java

黄舟
黄舟Original
2017-01-17 15:21:061620browse

Ask a question

What are the situations of active use and passive use of Java classes? ? ?

Solve the problem

Active use of Java classes will lead to the initialization of the class:

1) Create an instance of the class

2) Access a certain class Or a static variable of the interface, or assign a value to the static variable

3) Call the static method of the class

4) Reflection (such as Class.forName("com.bunny.Test"))

5) Initialize a subclass of a class

6) The class that is indicated as the startup class (JavaTest) when the Java virtual machine starts Are considered passive uses and do not cause the class to be initialized.

[code]package com.evada.de;

class ChildClass extends  ClassUsed{
    public static int c = 0;
}

/**
 * Created by Ay on 2016/5/24.
 */
public class ClassUsed {

    public static int a = 0;

    public static void main(String[] args) throws Exception{

        /** 创建类的实例  **/
        ClassUsed classUsed = new ClassUsed();

        /** 访问某个类或接口的静态变量,或者对该静态变量赋值 **/
        int b = ClassUsed.a;

        /** 调用类的静态方法  **/
        ClassUsed.test();

        /** 反射 **/
        Class.forName("com.bunny.Test");

        /** 初始化一个类的子类 **/
        ChildClass.c = 10;

        /** Java虚拟机启动时被表明为启动类的类 **/
        //java com.hwy.MyTest
    }

    public static void test(){

    }
}

The above is the content of active and passive use of Java. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

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