Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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

 
Greenhorn
Posts: 5
  • 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: 24089
54
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: 5485
55
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.
 
Master Rancher
Posts: 3887
45
  • 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: 62881
203
  • 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: 24089
54
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: 62881
203
  • 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
Master Rancher
Posts: 3887
45
  • 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:
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!