简体   繁体   English

如果我从大小变化中派生出java对象,是否需要重建?

[英]Do I need to rebuild if a java object I derive from changes in size?

I tried searching for an answer and was unsuccessful. 我尝试寻找答案,但未成功。 If a class which I derive from which is contained in a jar changes in size, do I need to rebuild my code? 如果从罐中包含的派生类的大小发生变化,是否需要重建代码? By the way, I'm new to java. 顺便说一句,我是java的新手。

I'm pretty sure in C++ the answer is yes. 我很确定在C ++中答案是肯定的。 I think a 'new' in C++ consists of a malloc() followed by a call to the constructor. 我认为C ++中的“新”包含malloc(),然后调用构造函数。 If the size of the class I derive from changes in the binary I'm linking with I would need to recompile my source code so that the compiler knows the new size of the class I'm deriving from otherwise not enough memory will be allocated. 如果我要链接的二进制文件中的更改导致类的大小,则需要重新编译源代码,以便编译器知道我从中派生的类的新大小,否则将无法分配足够的内存。

[edited with example] [示例编辑]

Bear with me as I'm not that familiar with java. 忍受我,因为我对Java不那么熟悉。 Ignore any syntax errors. 忽略任何语法错误。 Hopefully I'll get the example close enough to get across my point. 希望我能使示例足够接近,以阐明我的观点。

public class MyClass extends 3rdPartyClass
{
...
}

public class OneOfMyClasses
{
   public void Foo()
   {
      MyClass mc = new MyClass();  <-- allocation needs to happen here
   }
}

In 3rdParty jar file: 在3rdParty jar文件中:

public class 3rdPartyClass
{
   private int int1;
   private int int2;

...
}

Now a change is made in the 3rdPartyClass. 现在,在3rdPartyClass中进行了更改。 They add some more instance members: 他们添加了更多的实例成员:

public class 3rdPartyClass
{
   private int int1;
   private int int2;
   private int int3;
...
}

There is one more int and thus when the allocation for MyClass happens it needs to change from x bytes to x + sizeof(int) bytes. 还有一个int,因此在分配MyClass时,它需要从x字节更改为x + sizeof(int)字节。 Is the allocation (malloc?) happening in my code? 分配(malloc?)是否在我的代码中发生? In C++ the malloc happens in my code and then the call is made to the constructor. 在C ++中,malloc发生在我的代码中,然后调用构造函数。 Thus my code would have to get recompiled, even though I didn't change anything, such that the correct number of bytes could be allocated. 因此,即使我没有进行任何更改,我的代码也必须重新编译,以便可以分配正确的字节数。 Just wondering how it works in java. 只是想知道它如何在Java中工作。

Thanks, Nick 谢谢,尼克

If I understood you correctly, no. 如果我对您的理解正确,则不会。 If a jar/lib you are using changes the implementation/code you don't have to rebuild your own classes which extend from them. 如果您使用的是jar / lib,则无需更改实现/代码,就不必重建自己的类。

If the lib changes public members you have to recompile. 如果lib更改了公共成员,则必须重新编译。

There are some pitfalls though, I don't remember correctly. 但是有一些陷阱,我记不清了。 But in general I would propose to recompile your code always. 但总的来说,我会建议始终重新编译您的代码。

Java programs use always the standard Java API classes and interfaces which are normal classes in a normal jar. Java程序始终使用标准Java API类和接口,它们是普通jar中的普通类。 If you distribute a jar of your software compiled with an implementation from one vendor you can let that run against implementations from other vendors. 如果分发一罐软件,其中包含一个供应商的实现编译的软件,则可以使其与其他供应商的实现运行。 This is quite an important aspect of Java which would not work if memory usage details of one implementation would be used by extending classes. 这是Java的一个重要方面,如果扩展类使用某个实现的内存使用详细信息,它将无法正常工作。 The bytecode compilation is completely free of such stuff. 字节码编译完全没有这种东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要在这里重新构建Java吗? - Do I need a rebuild for Java here? 每次从Merurial存储库中提取Java项目时,我都需要重建Java项目吗? - do i need to rebuild java projects everytime I pull it from a mercurial repository? Java:我是否需要保护垃圾收集器中的Thread对象? - Java: do I need to protect a Thread object from the Garbage Collector? Java 和 Docker 的缓慢开发工作流程/反馈循环:我是否需要在每次更改后重建 JAR 和图像? - Slow development workflow/feedback loop for Java with Docker: do I need to rebuild the JAR and image after every change? JAVA:更新context.xml中的密码后是否需要重建应用程序? - JAVA: Do i need to rebuild an application after updating password in context.xml? 如何使用Java标准API从字符串中导出特定数据? - How do I derive specific data from a string using the Java standard API? 如何使用Java标准API从字符串中导出特定数据? - How do I derive specific data from a string using the Java standard API? 我真的需要定义java最小堆大小 - Do I really need to define java minimum heap size 我需要在java中创建json对象。 我将如何做? - I need to create json object in java. How will I do it? 如何使其他类派生自单例类? - How do I make other classes derive from a singleton class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM