• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Quick question about enum field

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if I'm just very tired atm or what, but I can't get this little code to work...



The test is simply to first declare an enum variable, and then change ("construct") it on a separate line, the line after. Writing the "construction", Sizes size = Sizes.M; , on one line works fine but this doesn't, why?

// Andreas
 
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On each line immediately after your class declaration, you're (a) declaring a field and (b) (optionally) initializing the field just declared.

The line 'public static Sizes size;' declares a field and declines the option to initialize explicitly.
The line 'public static Sizes size = Sizes.M;' declares a field and exercises the option to initialize explicitly.
The line 'size = Sizes.M;' declares no field....

Assign a value to an already declared field either in a constructor or a method, if you haven't explicitly initialized it and its default value (if any) is no longer what you need.
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
put in simple words

enum constats are public static and final and we cannot change the value of the final variable
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Byron wrote:On each line immediately after your class declaration, you're (a) declaring a field and (b) (optionally) initializing the field just declared.

The line 'public static Sizes size;' declares a field and declines the option to initialize explicitly.
The line 'public static Sizes size = Sizes.M;' declares a field and exercises the option to initialize explicitly.
The line 'size = Sizes.M;' declares no field....

Assign a value to an already declared field either in a constructor or a method, if you haven't explicitly initialized it and its default value (if any) is no longer what you need.



Yeah I just realized where I was trying to initialize that enum was probably the problem, as you just verified for me
Never really thought about that type of initialization in the class body before, outside a method/constructor but without a declaration... I guess it's just one of those silly things you never do when you actually code something,
but it's a good thing to have verified that it's not possible to do. Thanks.

// Andreas
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic