• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

OCP Practice Exams Sef-Assessment Test 2 Q4

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code fragment below :



I have the following questions:

1.Are Enums always declared outside a class in Java?
2. why did the print out in the main print the following : C C 1 1? I thought the counter was increasing.. so that it becomes C 1 C 2. Someone please explain this to me.

Thanks
 
author & internet detective
Posts: 40791
828
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be more explicit about the source: K&B, practice exams, self assessment test #2.

1) No. When you learn about inner classes, you will see an enum can be an inner class instead of outside a class. For example, this code is fine:


2) An enum creates all the values at the same time, not when you first reference them. This means the two constructors are called one after another. And both are called before the println runs to output the counts.
 
henry joe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:To be more explicit about the source: K&B, practice exams, self assessment test #2.

1) No. When you learn about inner classes, you will see an enum can be an inner class instead of outside a class. For example, this code is fine:


2) An enum creates all the values at the same time, not when you first reference them. This means the two constructors are called one after another. And both are called before the println runs to output the counts.



So, basically, the System.out.println("c ") statement is called one for RAINY and then for SUNNY after which the count is incremented and again, called on the RAINY and then the SUNNY. So, it is calling both on each statement. Whould I be correct IF the statement had been this :


AND THEN..calling this should give : c c 2 2 p p. Am I correct with this assumption?
 
Jeanne Boyarsky
author & internet detective
Posts: 40791
828
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try it and see what the output is. Asking why on the internet is more helpful than asking strangers to run your test examples for you.

I'll pretend you did that and explain why that is not the output.

The constructor for both enums runs. Which prints cp twice and increments count. Then the println with the count in it runs.
 
henry joe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:You could try it and see what the output is. Asking why on the internet is more helpful than asking strangers to run your test examples for you.

I'll pretend you did that and explain why that is not the output.

The constructor for both enums runs. Which prints cp twice and increments count. Then the println with the count in it runs.



Hi, I am sorry, I keep bugging you with the enum issue. Ok, I did test it and found out it goes this way : [c p c p 2 2]

It seems that even though the count++ statement came before the System.out.println("p ") statement, the out seems to work by printing out the strings first, only then is the count increment printed. I am trying to understand if this is how the enum constructor is built to work or something else is happening here. I mean, it is not going sequentially any more but jumping the count++ to the statement below that. This is what I am trying to understand.

Thanks
 
Jeanne Boyarsky
author & internet detective
Posts: 40791
828
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

henry joe wrote: I mean, it is not going sequentially any more but jumping the count++ to the statement below that. This is what I am trying to understand.


It is going sequentially. Here's what happens.

1) Java creates RAINY
a) prints c
b) Changes RAINY's count value to 2
c) prints p
2) Java creates SUNNY
a) prints c
b) Changes SUNNY count value to 2
c) prints p
3) Calls main method and prints both 2's

Another way to look at it is to try running this enum:


It prints the following which shows count++ is in fact run in order.
c : 1
p : 2
c : 1
p : 2
2 2
reply
    Bookmark Topic Watch Topic
  • New Topic