Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing an enum inner class from a different class  RSS feed

 
Chad Schultz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I know this is a stupid question...

It's easy enough to access an inner class in general. From a different class, you can refer to OuterClass.InnerClass. Easy as that.

However, I'm using an enum inner class. OuterClass.InnerEnumClass.InnerEnumMember doesn't work. Neither does Outerclass.InnerEnumMember. What's the syntax, then?

Let's use this as an extremely oversimplified example;



In this example, references FROM the outer class (dir = Direction.LEFT) work just fine. However, when I need to access it from another class, I cannot seem to use the enum (new OuterClass(Direction.RIGHT)). What's the proper syntax?

Thanks!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nested enums are implicitly static. Therefore the word "inner" does not apply, by definition. And therefore you can't access them using an instance of the outer class - instead, use the slightly simpler syntax
like this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!