-1

I have a class i.e. Category. Its a POJO with some member variables and getters and it can be instanciated. Category contains a static nested class called Contract. The Contract contains only constants like column names of a database table. Last but not least contains the Category also a static Builder to configure a new Category and instanciate this one.

class Category {
   static class Contract{}
   static class Builder{}
}

Can their be a problem about performance or memory with this class structure? I mean by 100 Categories and each one has a Contract and a Builder. Any help or infos are welcome.

Happo
  • 1,375
  • 3
  • 16
  • 34
  • Possible duplicate of [What is the memory consumption of an object in Java?](http://stackoverflow.com/questions/258120/what-is-the-memory-consumption-of-an-object-in-java) – shmosel Jan 06 '17 at 23:13
  • 1
    There is no such thing as a "static inner class" in Java. "The static keyword may modify the declaration of a member type C within the body of a non-inner class or interface T. Its effect is to declare that C is not an inner class." https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.5.1 "An inner class is a nested class that is not explicitly or implicitly declared static." https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.1.3 – Lew Bloch Jan 06 '17 at 23:17
  • you mean 100 Category instances or 100 classes like Category? also which kind of performance or memory impact you are concerned about? any background of what this is for? – Jason Hu Jan 06 '17 at 23:22
  • ['Static inner' is a contradiction in terms](http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.1.3). – user207421 Jan 07 '17 at 01:34
  • Sorry for the confusion about "inner" and "nested" class. I changed the title and description. – Happo Jan 09 '17 at 05:31

2 Answers2

0

Whether or not you have a performance or memory issue is entirely a matter of how the classes are defined and used, and is not affected by whether they're nested classes or not.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10
0

No instance of the static nested classes will be created when you create an instance of the Category class. So no memory impact (the code will behave, from a memory perspective, as if the nested classes had been declared as top level classes).

Had they been inner classes (not static) the answer would be different.

assylias
  • 321,522
  • 82
  • 660
  • 783