Question

So when you make an instance of a class such as:

class Example {

     public static void main(String[] args) {
          Example example = new Example();

     }

}

Will anymore resources be used if I did:

class Item {

     public Item() {
          //empty
     }

}


class Example extends Item {

     public static void main(String[] args) {
          Example example = new Example();

     }

}

If so, why?

Was it helpful?

Solution

There is an insignificant increase in memory usage due to the fact that you have an extra class to load. If you have 100 subclasses of Item, you would have 100 extra classes to load ... and if you take it far enough the extra memory usage for the extra code will be significant.

However, an instance of Example will occupy the same space as an instance of Item because Example does not declare any instance fields. Note that extra methods or method overloads do not contribute to the size of an instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top