首页 \ 问答 \ 我们什么时候说java中加载了类?(When do we say class is loaded in java?)

我们什么时候说java中加载了类?(When do we say class is loaded in java?)

当我们说一个class被“加载”时,它意味着什么?

例如,据说Static init块在类加载时执行。 什么时候加载了class

请参阅Kathy Sierra的书中的代码:

class Bird {
    {
        System.out.print("b1 ");
    }
    public Bird() {
        System.out.print("b2 ");
    }
}

class Raptor extends Bird {
    static {
        System.out.print("r1 ");
    }
    public Raptor() {
        System.out.print("r2 ");
    }
    {
        System.out.print("r3 ");
    }
    static {
        System.out.print("r4 ");
    }
}

class Hawk extends Raptor {
    public static void main(String[] args) {
        System.out.print("pre "); //1
        new Hawk();//2
        System.out.println("hawk ");
    }
}

上面代码的输出是: r1 r4 pre b1 b2 r3 r2 hawk

我不能理解在r1之后打印prefor的结果。 哪部分代码加载了Raptor类?

其中一个答案是:

“该类在最后一刻被初始化,但在访问其任何成员之前”。

但按照这个逻辑,不应该在r1之前预先打印? 由于第2行在执行line1后访问类Raptor的构造函数。


What does it mean when we say a class is "loaded"?

For example it is said that Static init blocks are executed at the time of class loading. When exactly class is loaded?

Please see this code from Kathy Sierra's book:

class Bird {
    {
        System.out.print("b1 ");
    }
    public Bird() {
        System.out.print("b2 ");
    }
}

class Raptor extends Bird {
    static {
        System.out.print("r1 ");
    }
    public Raptor() {
        System.out.print("r2 ");
    }
    {
        System.out.print("r3 ");
    }
    static {
        System.out.print("r4 ");
    }
}

class Hawk extends Raptor {
    public static void main(String[] args) {
        System.out.print("pre "); //1
        new Hawk();//2
        System.out.println("hawk ");
    }
}

The output of above code is: r1 r4 pre b1 b2 r3 r2 hawk

I cant not understand howcome pre is printed after r1. Which part of the code loaded Raptor class?

One of the answer says :

"the class is initialized at the last possible moment, but before any of its members is accessed".

But by that logic, shouldn't pre be printed BEFORE r1? Since line 2 accesses constructor of class Raptor AFTER execution of line1.


原文:https://stackoverflow.com/questions/39412177
更新时间:2023-09-29 12:09

最满意答案

Hex-Rays反编译器是一个很好的工具,但代码很难读取,你将不得不花费大量的时间对整个DLL进行反向工程。


Hex-Rays decompiler is a great tool, but the code will be quite hard to read and you will have to spend a lot of time to reverse engineer the whole DLL.

相关问答

更多
  • 不要反套。 只需逐渐用C替换组件即可。以下是一系列可能的转换。 .LFBO pushq %rbp movq %rsp,%rbp movl %edi,-4(%rbp) movl %esi,-8(%rbp) movl -4(%rbp),%eax compl -8(%rbp),%eax jg .L2 movl -8(%rbp),%eax jmp .L3 .L2: movl ...
  • Hex-Rays反编译器是一个很好的工具,但代码很难读取,你将不得不花费大量的时间对整个DLL进行反向工程。 Hex-Rays decompiler is a great tool, but the code will be quite hard to read and you will have to spend a lot of time to reverse engineer the whole DLL.
  • 严格回答你的问题,你需要添加一个返回构造函数结果的extern“C”函数: extern "C" foo* __declspec(dllexport) new_foo(int x) { return new foo(x); } 然后在源文件中,您可以使用“new_foo”上的GetProcAddr来调用该函数。 Answering your question strictly, you need to add an extern "C" function that returns the resu ...
  • 首先,你的代码中混合了各种值。 %esi以值b开始, %edi以值a开始。 您可以从testl %edx, %edx行推断%edx用作以.L7开头的循环的条件变量(如果%edx不.L7 0,则控制权将转移到.L9块中,然后返回.L7 )。 我们将在我们的反向工程代码中将%edx称为remainder 。 我们开始逆向设计主循环: movl %edi, %edx 由于%edi存储a ,所以相当于用remainder初始化remainder数值: int remainder = a; 。 movl ...
  • 您可以直接使用PInvoke和DllImport属性来调用它。 当然,假设它有标准的C入口点,而不是编译器特定的C ++入口点。 除非你在谈论C ++ / CLI,在这种情况下,你会引用.NET DLL,就像你引用任何其他.NET DLL一样。 You can call it directly with PInvoke and the DllImport attribute. Assuming, of course, that it has standard C entry points, not comp ...
  • 检查汇编代码。 把它变回C ++。 利润。 不可否认,你可以在第1步和第2步中添加一些细节,但这是基本的想法,我的答案中的详细程度超过了你问题中的详细程度:-) Examine the assembler code. Turn it back into C++. Profit. Admittedly, there a fair bit of detail you could add to steps 1 and 2 but that's the basic idea, and the level of de ...
  • 获取: http : //www.dependencywalker.com/ ,使用depends.exe打开DLL,然后在“视图”菜单中激活“Undecorate C ++ Functions”。 我主要使用它来查找依赖项,但它也暴露了DLL的入口点。 这不是万无一失的,因为暴露类的DLL不必导出其方法。 例如,纯虚方法布局足够统一,您可以将实例公开为可能是工厂函数的接口指针。 但它可能会解决您的问题。 无论如何,您需要一个依赖性walker的副本。 :) Get this: http://www.dep ...
  • 你几乎做对了。 只需将数字2和3放在同一个C ++ / CLI DLL中。 您既可以使用.NET类,也可以从C ++ / CLI项目中导出C函数。 extern "C" __declspec( dllexport ) void __stdcall PureExportC( const wchar_t* param ) { CSharpAssemblyNamespace::CSharpWorker ...
  • 如果将宏定义转储到文本文件会有所帮助,请尝试以下方法: application.saveastext acMacro, "Macro1", "Macro1.txt" 然后,您可以打开文本文件并阅读Action和Argument语句。 If it would be helpful to dump the macro definitions to text files, try something like this: application.saveastext acMacro, "Macro1", "Ma ...
  • 答案是两者。 使用CLR的方法将编译为il,而不使用clr的方法将编译为本机代码。 但最好的答案是编写一些示例函数,编译dll并在Refelctor中打开它。 The answer is both. Methods that use CLR will be compiled as il, while methods that do not use clr will be compiled to native code. But the best answer for you will be to write ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)