This week's giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Production-Ready Serverless (Operational Best Practices) and have Yan Cui on-line!
See this thread for details.
Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

How do I set an enum value outside a java class  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a class Tilemap in where I have an enum and a 2d array. The array is an array of the enum.


In my main method I have an instance of Tilemap called Tilemap. I then try to set one of the values of the 2d array to a value of the enum but it won't work.

It says "class or package expected"

Does anybody know what I am doing wrong?
 
Sheriff
Posts: 24282
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't set an enum value outside of a class. In fact you can't do anything outside of a class, and that's what the error message is telling you. That line of code should be inside a class.
 
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would have to be "static" because you are attempting to access it using the class name.

Also, "state" should begin with an upper case letter.
 
Rancher
Posts: 3983
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Would have to be "static" because you are attempting to access it using the class name.



According to Luc:

In my main method I have an instance of Tilemap called Tilemap.



Which means that it doesn't have to be static as the instance has the same name as the class.

Proper naming standards mean, of course, that the variable should be tilemap to avoid the confusion.
 
Marshal
Posts: 63781
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is usually very bad design to have a public variable field anywhere. You aren't using it in the Tilemap enum, but elsewhere, so make it private and declare it elsewhere.
If you are making a game, then you would have a Board class which has such an array as a private field.
 
Paul Clapham
Sheriff
Posts: 24282
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, yes, it's true that that variable should be static. But failing to declare it static doesn't cause the compiler to produce the error "class or package expected". That error is produced because that line of code is outside of the class declaration.

So Luc, please post the code for the class for which the compiler produces that error.
 
Campbell Ritchie
Marshal
Posts: 63781
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hadn't noticed the bit about that compiler error; sorry.
If you get, “class, enum, or interface expected,” at the beginning of the class, it usually means you have a spelling error somewhere, e.g. writing Class instead of class. If you get the error at the end of the class, it usually means you have too many }s somewhere. Only correct indentation will allow you to correct that error properly.
 
Dave Tolls
Rancher
Posts: 3983
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taking the Tilemap class as presented, when I do this in another class:


the compiler assumes that the Tilemap tokens in line 2 refer to the variable declared in line 1 and not the class name.
This is correct in the first case, where it is referencing the array tiles, but not in the second, where it's trying to get at the enum.

This is why I said that naming standards are important.
In this particular case it is confusing the compiler, and not just us.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Taking the Tilemap class as presented, when I do this in another class:
the compiler assumes that the Tilemap tokens in line 2 refer to the variable declared in line 1 and not the class name.
This is correct in the first case, where it is referencing the array tiles, but not in the second, where it's trying to get at the enum.

This is why I said that naming standards are important.
In this particular case it is confusing the compiler, and not just us.


Agreed about naming conventions, it IS important for sake of readability. Also it can be helped by importing state using full qualified name:
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!