简体   繁体   中英

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.

2)Using inner java class.

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.

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. 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. It's a bad code smell.

There is no difference between creating two classes in same file and creating inner 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 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 .

There is no matter of performance issue,Since once you compile that file a separate .class generates for each class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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