小编fek*_*kir的帖子

如果相应的类从未使用过,类加载器是否会加载类文件?

为了使我的问题更清楚,请考虑以下用例:

假设有一个包允许在给定平台上进行一组操作,例如在 Windows 上编辑注册表的类。这个包在其他平台上不存在,因为在其他操作系统上没有等效的操作。

为了简单起见,考虑

窗口/Registry.java

package windows;

public class Registry {

  static Registry instance = null;
  static{
    System.out.println("print from static block");
  }

  private Registry() {
    System.out.println("Registry instance created!");
  }

  public static synchronized Registry getInstance() {
    if (null == instance) {
      instance = new Registry();
    }
    return instance;
  }

  public void foo() {
    System.out.println("foo called.");
  }
}
Run Code Online (Sandbox Code Playgroud)

以及我将有条件使用注册表的类:main/Main.java

package main;

import windows.Registry;

public class Main {

  public static void test1(boolean onWindows) {
    if (onWindows) {
      Registry instance = Registry.getInstance();
      System.out.println("We …
Run Code Online (Sandbox Code Playgroud)

java classloader

3
推荐指数
1
解决办法
348
查看次数

与友元类不同的两个类声明是否会违反 ODR?

考虑a.hpp

class foo{
  int c;
};
Run Code Online (Sandbox Code Playgroud)

b.hpp

class bar;
class foo{
  friend bar;

  // from here identical to a.hpp
  int c;
};
Run Code Online (Sandbox Code Playgroud)

严格来说,这是否违反 ODR?

c++ one-definition-rule

1
推荐指数
1
解决办法
78
查看次数

标签 统计

c++ ×1

classloader ×1

java ×1

one-definition-rule ×1