• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Should it not give compile time error?

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above code points has been declared as of private type and it is being accessed from the main method with the following syntax:
System.out.println(Suits.SPADES+" "+Suits.SPADES.points);

Should it not give compile time error at line no. 20.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it shouldn't, the enum is declared in the same class as where the main method is.
If you put the enum outside the class, you will get a compile time error that points isn't accessible.
 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

indra negi wrote:
System.out.println(Suits.SPADES+" "+Suits.SPADES.points);
Should it not give compile time error at line no. 20.




here the main() method is inside the same class in which you have declared the enum....
i suppose that you already knew that its perfectly legal to access a private member inside the class in which it is declared....

adding to it you should know that all enums are almost like classes which implicitly extend java.lang.Enum and that their constructors can't be invoked explicitly.....

i suppose that you will understand this clearly...
Thank you!
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Thomas and Karthick. You are right. I got it.
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

indra negi wrote:Thanks Thomas and Karthick. You are right. I got it.


you are welcome friend.......
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can someone explain the out put given by the

System.out.println(Suits.values());

it gives '...Bridge$Suits;@1372a1a' as output.

What's this ?

/Rashmi.
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well this is the default value returned by the toString method. You don't need to understand @1372a1a came from. Before it is the enum name (Bridge is the containing class of Suits enum so Bridge$Suits) and after that is a hash code. If a question like this comes, you just have to know that there is a weird looking value that the default toString method returns...
 
Rashmi Liyan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit,
thanks . I got it.

/Rashmi
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic