• Post Reply Bookmark Topic Watch Topic
  • New Topic

Enums and size  RSS feed

 
Lane Meyer
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a few questions about enum size when it come to declarations.

1: If I declare an enum in a class that will be instantiated multiple times, will that be reflected in memory?

Example 1:


will a,b,c take up storage in each instance? IE multiple copies of the enum.

what if it's declared like this:

Example 2:


My assumption is no but I want to be sure.

2. What if I add a final variable like this;

Example 3:


If i remember right when Foo(int enum_var) is called it just creates a group of ints representing those values, not creating a separate "instance" for lack of a better word atm.
So it wouldn't create multiple copies of the array correct?

Again my assumption is no but I want to be sure.

Edit:
I am operating under the assumption that, like static variables, they belong to the class not the instance IE they will not be copied multiple times.
 
Lane Meyer
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I might have found my own answer.

Enums are implicitly static as a member of a class, so they belong to the declaration and not the instance.
According to http://www.javapractices.com/topic/TopicAction.do?Id=1
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice link.

There are more details in the Java™ Language Specification, where we find that enum elements are implicitly Singletons, so they cannot occupy several places in memory. that is why you can use the == operator.
 
Lane Meyer
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for that link Campbell. I've been looking for a section like that. I'm not a native java programmer and I have never taken the time to truly understand the language until now. In my computer science program, mastering the language is left up to the student. I've spent so much of my time in other pursuits, until now I haven't looked to create elegant solutions and confirming my assumptions.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

. . . and, since you're new, welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!