简体   繁体   English

静态块未运行,没有最终变量

[英]Static Block not running, no final variables

I have this code here using my API: 我在这里使用我的API编写以下代码:

package org.midnightas.os.game.dots;

import java.awt.Graphics2D;

import org.midnightas.os2.Key;
import org.midnightas.os2.MidnightasOS;
import org.midnightas.os2.gameapi.Game;

public class Dots extends Game {

    public Dots(MidnightasOS midnightasos) {
        super(midnightasos);
    }

    @Override
    public void init() {

    }

    @Override
    public void keyPressed(Key arg0) {

    }

    @Override
    public void render(Graphics2D arg0) {

    }

    @Override
    public void tick() {

    }

    static {
        System.out.println("MOS Dots crashed.");
        MidnightasOS.setGame(Dots.class);
    }

}

The static block is supposed to be ran calling MidnightasOS.setGame(Class); 应该运行该静态块并调用MidnightasOS.setGame(Class);。 However that is not happening. 但是,这没有发生。
I have also debugged using System.out to no avail. 我也使用System.out调试无济于事。
Is the problem within MidnightasOS? MidnightasOS内的问题吗? I will post it's code if necessary. 如有必要,我将发布它的代码。

I'm doing this because I'm trying to create an artificial operating system with Linux and the Raspberry PI. 我这样做是因为我正在尝试使用Linux和Raspberry PI创建一个人工操作系统。
This shall be a game console like the Game Boy. 这应该是像Game Boy这样的游戏机。
I'm trying to load all Game classes so at least one of them would use MidnightasOS.setGame(Class); 我正在尝试加载所有Game类,因此至少其中一个将使用MidnightasOS.setGame(Class);

Thanks for reading. 谢谢阅读。

When is Dots class loaded by classloader. 什么时候由classloader加载Dots类。 It will be loaded on the first reference of this class. 它将在此类的第一个引用上加载。 See if you ever refer to this class 看看你是否曾经提到过这堂课

You can even dynamically load the class And to find all the subtypes of a class and load them all you can use this library 您甚至可以动态加载类,并找到类的所有子类型并将其全部加载,您可以使用此库

 public class MainClass {

  public static void main(String[] args){

    ClassLoader classLoader = MainClass.class.getClassLoader();

    Reflections reflections = new Reflections("org.midnightas");

    Set<Class<? extends Game>> subTypes = reflections.getSubTypesOf(Game.class);
    for(Class<? extends Game> subType : subTypes){
       try {
          Class aClass = classLoader.loadClass(subType);
          System.out.println("subType.getName() = " + subType.getName());
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      }
   }

}

The static blocks in a class are executed as soon as the classloader loads the class for the first time. 当类加载器首次加载该类时,将立即执行该类中的静态块。 There are several possibilities to achieve this. 有几种方法可以实现这一目标。 Consider the following class: 考虑以下类别:

public class SomeClass {

    static {
        System.out.println("static block in SomeClass");
    }

    static void someMethod() {
        System.out.println("some static method");
    }
}

  1. Loading it by creating an object: 通过创建一个对象来加载它:

SomeClass foo = new SomeClass();

  1. Loading it by calling a static method: 通过调用静态方法加载它:

SomeClass.someMethod();

  1. Loading it directly: 直接加载:

Class.forName("SomeClass");

These are only some of the possibilities you have! 这些只是您拥有的一些可能性! Please remark that you'll have to include the package structure into the third approach (if the class is in the package some.package it'll be: Class.forName("some.package.SomeClass"); 请注意,您必须将包结构包括在第三种方法中(如果类在包some.package中,则为: Class.forName("some.package.SomeClass");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM