简体   繁体   English

在单个Java文件中使用两个Java类的性能问题

[英]Performance issue in using two java class in single java file

I just wanted to know that is there any performance issue in 我只是想知道那有什么性能问题

1)Using two classes in single java file. 1)在单个java文件中使用两个类。

2)Using inner java class. 2)使用内部java类。

I want to use it for internal data structure and supporting class. 我想将其用于内部数据结构和支持类。 Also please tell me which one will be better in terms of performance? 还请告诉我哪一个在性能方面会更好?

The fact that the classes source code is in a single Java file or in two separate Java files doesn't have any impact on the byte-code: both lead to exactly the same byte-code. 类源代码在单个Java文件中或在两个单独的Java文件中的事实对字节码没有任何影响:两者都导致完全相同的字节码。

An inner class is not different from any other class, except it has access to fields and methods of its enclosing class. 内部类与任何其他类都没有不同,不同之处在于它可以访问其封闭类的字段和方法。 The impact on performance is, basically, inexistent. 基本上,对性能的影响是不存在的。

I concur with @EricStein: don't worry about performance before you have a real performance problem. 我同意@EricStein的意见:在遇到实际的性能问题之前,请不要担心性能。 Strive for readability, maintainability and understandability. 力求可读性,可维护性和可理解性。 The standard is to have one top-level class per source file. 标准是每个源文件有一个顶级类。 You should respect this standard. 您应该遵守此标准。

Don't worry about performance until somebody notices a real problem in a production environment. 除非有人注意到生产环境中的实际问题,否则不要担心性能。 Anything else is premature optimization. 其他一切都是过早的优化。

That being said, don't use two top-level classes in a single java file. 话虽如此,不要在单个java文件中使用两个顶级类。 It's a bad code smell. 这是一种难闻的代码气味。

There is no difference between creating two classes in same file and creating inner class. 在同一文件中创建两个类与创建内部类之间没有区别。
Sine java compiler generates different .class files for every class irrespective inner class or not. Sine Java编译器会为每个类生成不同的.class文件,而与内部类无关。
So no difference at runtime . 因此在运行时没有区别。

First thing, 第一件事

I slightly disagree If You are comparing inner class and writing two classes in a single file . 如果您要比较inner class并在一个file编写两个classes ,我会略有不同。

Inner class is bounded to the Outer class as your design specification. 内部类绑定到外部类作为您的设计规范。

And writing two classes in same file is up to you(considering as a personal taste).Obviously vague and welcoming maintenance issues . 在同一个file编写两个类取决于您的个人喜好(视个人喜好而定)。显然是模糊且欢迎维护的issues

There is no matter of performance issue,Since once you compile that file a separate .class generates for each class. 毫无性能问题,因为一旦编译该文件,就会为每个class.生成一个单独的.class class.

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM