Can I have performance problems joining multiple classes to a single file?

0

Watching many tutorials and video lessons, I always see that a java file is created for each action / class. As indicated in the image below with the arrow number 1 yellow.

But as my application is getting huge, I'm starting to put the classes inside a single file, since each class will be used only in the option that is opened. Just like the arrows in red.

Now the question is: am I doing this correctly? Or could this make my application slower and should I do with each separate class in each file as it was before?

My idea is to simplify the best possible while maintaining a good execution speed.

    
asked by anonymous 21.02.2018 / 07:04

2 answers

4

The overview of @Pagotti's answer is right, if done right. However, their classes are not static. If it is not static, it is instance. Do you know what happens when something is instance? It needs to save the reference to the original instance.

How can I prove that it is an instance? In the inner classes, you can do FragmentList.this , which returns the reference to the FragmentList object that instantiated the class, for example, EquipeList .

Internally, how does it work? All built-in class constructors get one more argument 1 , FragmentList.this . Therefore, creating an object from an "instance" class imposes a more argument on the stack than a normal self-contained class constructor. It also has the question that each object will make one more reference, so there will be one more field inside each object.

What is the real impact of this? Just by measuring, I think it's next to nothing for normal applications. Note that games are not normal applications . A game needs to be optimized so the user does not have the perception of framemiss . That means you only have 16ms between two frames to render everything and provide a new screen (valid time for Android, I do not remember on other platforms). But is not usually this bit brushing that you can give the required performance.

    
21.02.2018 / 12:24
3

No difference in final performance

Separating a file for each class is a good practice to make code more readable and organized. The result of running the program after compiling will be the same if you have the classes in a single file or in several separate files.

    
21.02.2018 / 12:09