Java Programming

enum a cute data type in java to declare our own data type values

Why It Can't Be declared inside the Method ..................Why we Have to declare that inside the class

Replies

  • Ankita Katdare
    Ankita Katdare
    Hi @#-Link-Snipped-#
    We take it that you are new to java programming. But you can find the answers to these questions in reference books.
    Answering your query, the reason behind enum's non-ability to be declared inside a method is that enums are always static entities. What this means is that you must be able to access an enum from anywhere in your code, without involving the invocation of any code block (such as a method.)
    Isn't that the whole purpose behind declaring the cute enum keyword? 😀

    If an enum is defined inside a class, it becomes a static member of the class, and you can access it like any static member of the class such as static member classes of the class, as long as the class is accessible from your code.
    If enums were defined inside methods, then they wouldn't be static anymore. You would actually need to invoke the method in order to instantiate the enum.

    You should also keep in mind that - You can mark an enum which is declared inside a class as private or protected, so you must observe the access restrictions like for any other class member.
  • Dhaval Pujara
    Dhaval Pujara
    okk thanks

You are reading an archived discussion.

Related Posts

Sandisk CFast 2.0 Extreme Fast memory card is world's fastest and it's can write the bits at hair blowing 350 MB/s and retrieve them at 450 MB/s. These memory cards...
Below is a situation I have two database table 1: Posts 2: Recent Watched Now What I want is whenever user visits a certain post of mine, then it will...
Hi CEans, I had came over lots of places where I found that Women are more expressive about their thoughts,views,perceptions,etc. I want to stir up a debate on this topic...
Hewlett Packard or HP has recently launched its latest laptop the Chromebook 14 which will be known for featuring the 4th generation Dual-Core 1.1 GHz Intel Haswell processor. Alongside HP...
There are no examples of wheels in biological systems. One would think that gears would be even less likely. However, researchers have discovered a gear mechanism in a jumping insect....