aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enums and operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enums and operators" Watch "Enums and operators" New topic
Author

Enums and operators

kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
hi all im unable to understand this program especially in the for loop part..what is the use and working of this type of for loops..can any one explain the program in detail
3. public class McGee {
4. public static void main(String[] args) {
5. Days d1 = Days.TH;
6. Days d2 = Days.M;
7. for(Days d: Days.values()) {
8. if(d.equals(Days.F)) break;
9. d2 = d;
10. }
11. System.out.println((d1 == d2)?"same old" : "newly new");12. }
13. enum Days {M, T, W, TH, F, SA, SU};
14. }

the result is same old

thanks in advance
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
It's called "enhanced for loop" or the "for each loop". If you search these terms you will hit good results.

Read Mathew's reply in this thread - http://www.coderanch.com/t/549973/java/java/Enhanced-loop

for-each loop is used to iterate over a collection or an array.

Below is a sample code to demonstrate how to do use the for-each loop on arrays & collections.



When you call the static method values() of an enum (here the Days enum) you get an array of the enum constants. And this array is looped through using for-each in your example.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
As a friendly reminder, there are two points I want to add about this question. In the exam, there may be a choice for " compilation error " .


public class McGee {
public static void main(String[] args) {
Days d1 = Days.TH;
Days d2 = Days.M;
for(Days d: Days.values()) {
if(d.equals(Days.F)) break;
d2 = d;
}
System.out.println((d1 == d2)?"same old" : "newly new");12. }
enum Days {M, T, W, TH, F, SA, SU};
}


1. All enum declared inside a class , are implicitly static.

2. How about adding a private method inside the enum?

enum Days {M, T, W, TH, F, SA, SU

private void print(){ ....}
};

Can we invoke d.print() in the above main method as print is private? Yes. Like an inner class, the outer class can access inner class's private method.

In the exam, if this question is changed into this with Days declared outside the class McGee:


enum Days {M, T, W, TH, F, SA, SU};

public class McGee {
static Days d1, d2 ;
public static void main(String[] args) {
d1 = Days.TH;
d2 = Days.M;
for(Days d: Days.values()) {
if(d.equals(Days.F)) break;
d2 = d;
}
System.out.println((d1 == d2)?"same old" : "newly new");12. }
}

The enum Days is declared outside McGee class and Days d1, d2 are declared outside the static main method. Days d1 and d2 have to be declared static in order to be used in static method. So, enum Days declared outside the class is NOT implicitly static. If Days has a private print() method, this print() method cannot be called from McGee, just like accessing a private method of a class.

Correct me if I am wrong.
kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
thanks can anyone explain the function of this satatement " if(d.equals(Days.F)) break;"
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
For enum comparison, .equals and == works.

for(Days d: Days.values()) {
if(d.equals(Days.F)) break;
d2 = d;
}


This code means for each Days enum, d , if d is F, then stop the loop.
For example:
1. d is M, the condition fails, d2 = d
2. d is T, the condition fails, d2 =d ;
....
5. d is F, the condition is true, stop the for loop and exit the for loop.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enums and operators
 
Similar Threads
Floating-point Operations
Behaviour of Enums
question about ==
Break statement in a for loop