• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enums and size

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome

. . . and, since you're new, welcome to the Ranch
reply
    Bookmark Topic Watch Topic
  • New Topic