2

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.

mid
  • 421
  • 5
  • 20

2 Answers2

3

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();
      }
   }

}
awsome
  • 2,143
  • 2
  • 23
  • 41
  • Then how do I load all classes that extend Game? All of this has to be dynamic. – mid Jan 22 '16 at 10:31
  • This would be too dynamic http://stackoverflow.com/questions/492184/how-do-you-find-all-subclasses-of-a-given-class-in-java – awsome Jan 22 '16 at 10:35
  • Can't I use Reflections? (I've seen someone do that once.) – mid Jan 22 '16 at 10:36
  • you can use this library to find all the subtypes .https://github.com/ronmamo/reflections But i think you are tacking your problem wrongly. May be add to your question the details as to why you want to do this. Why use a static block. May be someone will give you a better pattern to use – awsome Jan 22 '16 at 10:38
2

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");

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51