如何检测Java类是由其自己的main()调用还是从另一个类调用? [英] How can I detect if a Java class is called by its own main() or from another class?

查看:76
本文介绍了如何检测Java类是由其自己的main()调用还是从另一个类调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的Java类:

I have a Java class that looks like this:

public class My_ABC
{
  int a=0;
  boolean B=true;

  static  // Initialize and load existing data only once at start-up
  {
     // need to know if it's called from its own main()
     // or by another class to conditionally set veriables
  }

  public My_ABC(int AA,boolean BB)
  {

  }

  public static void main(String[] args) 
  {
    My_ABC my_abc = new My_ABC(3,true);
  }
}

因为在加载类时运行了静态块,如何检测它是从其自身的main()调用还是由另一个类调用以有条件地设置变量?

Because the static block is run when the class is loaded, how can I detect if it's called from it's own main() or by another class to conditionally set variables?

我知道你们中的一些人说:各种钟声响起!"好吧,这是因为我遇到了一种情况:我正在设计一个类,该类需要将大量数据加载到PC的极限(4G Ram)中,并且我正在运行只能使用1.5G的Java 32位版本的Ram max;因此,当我单独测试该类时,我需要加载尽可能多的数据以测试所有可能的情况,但是当从多个其他类中调用它时,它将无法执行此操作(将导致堆空间不足错误),因此只能加载最小需要的数据.但是,由于所有数据在启动时仅应加载一次,因此应将其保存在静态块中.否则,我需要实现额外的逻辑来检测它是第一次(需要加载数据)还是第二次,第三次(不应一次又一次地加载)进行加载.而且,如果我执行额外的逻辑来做到这一点并将数据加载代码移出静态块,则会导致不必要的复杂性,因为如果我移至64位版本的Java(希望很快),那么额外的复杂性将是额外的负担(我将有足够的空间来加载所有数据,即使从其他类中调用也是如此.因此,临时的快速修复方法是在静态块中检测出该异常并相应地处理差异,当我有足够的空间时,只需将它们注释掉,而无需更改编程逻辑结构.

I understand when some of you said "All sorts of bells go off!" Well, it's because I got a situation: I'm designing a class that needs to load lots of data to the limit of my PC (4G Ram), and I'm running 32-bit version of Java which can only use 1.5G of Ram max; so when I test this class by itself, I need to load as much data as possible to test all possible situations, but when it is called from multiple other classes, it can't do that (would cause out of heap space error), so it should only load min. data needed. And yet since all the data should only be loaded once at start up, it should be in the static block; otherwise I need to implement extra logic to detect if it's being loaded the first time (need to load data), or 2nd, 3rd time (shouldn't load data again and again). And if I implement extra logic to do that and move the data load code out of the static block, it would cause unnecessary complexity because if I move to 64-bit version of java (hopefully soon), that extra complexity would be extra burden (I'll have enough space to load all data even when being called from other classes). So the temp quick fix is to detect it in the static block and handle the difference accordingly, when I have enough space, just comment them out without the need to change programming logic structure.

感谢所有的答案和建议,我尝试了"StackTraceElement"方法,效果很好!它解决了我的问题.

Thanks for all the answers and advices, I tried the "StackTraceElement" approach, it works great! It solved my problem.

推荐答案

我认为您绝对应该更改您的方法.

I think you should definitely change your approach.

但是由于您>提出了一些具体建议这是(从其他人总结).

But since you asked something concrete here it is ( summarizing from others ) .

public class X { 
    static { 
        System.out.println("Static free block");
        StackTraceElement [] st  = new RuntimeException("").getStackTrace();
        if( st.length == 1 ) {
            System.out.println("Invoked from main");
        } else { 
            System.out.println("Invoked from somewhere else");
        }
    }
    public static void main( String [] args ) { 
        System.out.println("Main");
    }
}

使用它进行测试:

public class Y  { 
    public static void main( String [] args ) { 
        X x = new X();
    }
}

p.s. 我不知道为什么约瑟夫(Josef)删除他的答案是正确的.

p.s. I don't know why Josef delete his answer it was in the right track.

这篇关于如何检测Java类是由其自己的main()调用还是从另一个类调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆