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

Asked

Viewed 297 times

0

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

But as my app is getting huge, I’m starting to put the classes inside a single file, as each class will be used only on the option that opens. As I point with arrows in red colors.

inserir a descrição da imagem aqui

Now the question is, am I doing this right? Or this can slow down my application and I should do with each separate class in each file as it was before?

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

  • 1

    I believe that in this case the performance is not affected, but the organization of your project, this is not only for mobile applications, but for any type of project

  • I understand, I also think logic should not affect. I think bad java, that it is necessary to type 15 lines for a simple action. How much in php the same action could be done with a single line. Any simple application we create fills up with classes. So that’s why I asked this question, so that I do not have problems in the future as many users using. Thank you for answering friend.

  • 4

    Don’t worry about performance in advance. This will lead you to avoid perfectly valid code patterns and write less readable code. Write the most correct and clean code you can. If performance issues arise, take steps to determine where the problem is. Rarely will it be in that kind of detail.

  • 1

    It is also necessary to take into consideration that java will create separate bytecodes files for each class, even if they are in a single file, maybe it affects only the compile time, but the performance after compiled I believe not.

  • @The way he compiled it? If I’m not mistaken Java only accepts a public class (and non-public N) per file, and not N public classes (not speaking of Inner classes).

  • @Piovezan I can’t tell you because the image is blocked for me. If it’s two public classes, you’re right, don’t compile anyway, but you can stack several classes in one file, not all within the public class, myself I’ve done it here several times in answers

  • @Pabloalmeida thanks for answering. I’m just worrying now to avoid the most having to keep making changes afterwards. I’d rather go programming the right way. I’m putting everything in the same class exactly to avoid clutter with lots of files, but always have that fear of having to redo everything again. kkk,

  • @Articuno thanks for answering, I think that in the new version of android studio this already happens, give a look: https://image.ibb.co/gQWMYH/2018_02_21_13_54_54.jpg, I believe that’s what you’re saying. But if it doesn’t affect the performance ta great, the compilation for now is being fast.

  • @Piovezan I think you are right about several published classes in the same file, it even gives an error, but when you create one class inside the other, there works well. Take a look at this print where the error is and where it works: http://image.ibb.co/i5WwYH/2018_02_21_14_05_05.jpg

Show 4 more comments

2 answers

4


The overview of reply from @Pagotti is right if done right. However, your 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 of instance? In the internal classes, you can do FragmentList.this, which returns the reference to the object of FragmentList who urged the class, for example, EquipeList.

Internally, how does it work? All built-in class builders get an extra argument 1, the 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 a reference to more, so it will have one more field within each object.

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

  • 1

    Thanks for the reply buddy. I’m not making a game no. It’s a simple classified app. But it has many screens like FAQ, About the company, Contact Form, Parties and Events and etc. The question I started doing so is that each item that calls a list in the database or a json link, I need to create an Adapter class to popular the list. So instead of creating a file for each Adapter, I’m making it into the class responsible for each layout. Since it is something simple and I believe q is not heavy for a performance. But I understood your answer too. Thank you ^^

  • Is there any reference to what you’re saying, Jefferson?

  • @Caiquec. Bytecode scan? There is also the GWT part explaining that internal classes are not serialized automatically because they have no default constructor, as all constructors receive an implicit argument. I’ll look for a more canonical reference, however

  • @Caiquec. not yet canonical, but in other reference: https://stackoverflow.com/a/70613/4438007

  • @Caiquec. canonical reference: https://docs.oracle.com/javase/tutorial/reflect/member/methodparameterreflection.html#implcit_and_synthetic ; I’ll fit the answer now

  • @Jeffersonquesado, what I said is correct. The only thing I can’t understand is the relation of all this to having N classes in the same file.

  • @Caiquec. depends on the internal organization of the classes. If they are nested classes (most widely used strategy), static nesting avoids implicit parameters. Otherwise (less common strategy), they will be classes within the same compilation unit, so only one can be public and for all intents and purposes is not of instance (so it approximates to the static nested class). You have further details here: https://stackoverflow.com/a/23580793/4438007 . I particularly think that, since Oracle itself does not cite this second strategy, it is safe to ignore it

Show 2 more comments

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 compiled will be the same if you have the classes in a single file or in several separate files.

  • 1

    If the classes are static =/ non-static/instance class the performance is a little worse

  • Thank you for the friendly answer, I believe that for what I am doing, and for all that I have been answered on this topic, I really don’t think I will have any performance problems. I am calmer to follow the program. Thank you ^^

  • @Jeffersonquesado I had understood that his doubt did not refer to the structure of the classes but put itself in separate files or everything together. But reading your answer I understood that actually the question goes beyond because in the case of Java in the same file only fits internal class, I think if it is not internal it neither compiles.

  • @Pagotti, the case of having multiple classes as root has the same effect of being static classes. Maybe I have a visibility problem, I don’t really remember how to deal with it. I just know that it’s so rare to see it that I myself forget the existence.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.