简体   繁体   中英

Static Block not running, no final variables

I have this code here using my 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); However that is not happening.
I have also debugged using System.out to no avail.
Is the problem within 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.
This shall be a game console like the Game Boy.
I'm trying to load all Game classes so at least one of them would use MidnightasOS.setGame(Class);

Thanks for reading.

When is Dots class loaded by classloader. 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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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