2

Are there any notable advantages and/or disadvantages, expecially regarding performance, to replacing

private class MyClass{
    /**
    * Some code here
    **/

    private int numberOfPeople();
    private Human[] people;
    private void printPeople(){
        // some code here
    }

    /**
    * Some code here
    **/
}

with an inner class like this, that better encapsulates the data:

private class MyClass{
    /**
    * Some code here
    **/
    private class PeopleHandler{
        private int numberOfPeople();
        private Human[] people;

        private void printPeople(){
            // some code here
        }

        private void doOherStuff{
           // some code here
        }
    }

    /**
    * Some code here
    **/
}

I need to know this specifically for Java and Java Android.

kimv
  • 1,569
  • 4
  • 16
  • 26
  • Related: http://stackoverflow.com/questions/4953597/is-there-a-performance-overhead-to-a-private-inner-class-in-java – Andy Turner Dec 05 '15 at 00:11

1 Answers1

2

If you replace one class with another it makes little difference.

Using a nested class is about as expensive is adding a reference to a class. It could make a difference if you have many millions, but for most use cases you will have trouble measuring the difference.

I suggest you do what you believe is simplest and easiest to understand, and this is likely to perform well enough also.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks! Good, simple answer. Exactly what I needed – kimv Dec 05 '15 at 00:23
  • 1
    @kimv most performance problems are only apparent when you profile the application. You can guess what the problem is even if you have ten years experience in tuning Java application, but until you measure you don't really know. I can usually guess one or two things in the top 5 but I rarely get the 1st or 2nd without profiling. – Peter Lawrey Dec 05 '15 at 00:31