Content
1. What is Enum in Java?
2. Enum use in If Statement
3. Enum use in Switch Statement
4. Enum field
5. Enum method
6. Enum Iteration
1. What is enum?
An Enum in Java is:
- A special data type that enables for a variable to be a set of predefined constants.
- Its variable must be equal to one of the values that have been predefined for it.
- Enum able to contain field and method, etc and it was added in Java 5.
Example:
2. Enum use in If Statement
Developers like to enum to store constants variable for comparing by using If or Switch statement. So let's see enum as If Statement, we have Country enum that contains 3 fields:
If Statement:
3. Enum use in Switch Statement
Here is a sample, I had initialized it value "JAPAN". So our enum will run and check each value (PHNOM_PENH, JAPAN, KOREA). JAPAN's case will be true so it will set TextView to JAPAN on the screen.
4. Enum Field
We have 3 constructors that have each default value, if you use like this, it will return the default value, it has fruit field:
5. Enum Method
For enum method you can set/get the value from fruit fields like below:
6. Enum Iteration
You want to download this project from Github here! Thank You!
1. What is Enum in Java?
2. Enum use in If Statement
3. Enum use in Switch Statement
4. Enum field
5. Enum method
6. Enum Iteration
1. What is enum?
An Enum in Java is:
- A special data type that enables for a variable to be a set of predefined constants.
- Its variable must be equal to one of the values that have been predefined for it.
- Enum able to contain field and method, etc and it was added in Java 5.
Example:
2. Enum use in If Statement
Developers like to enum to store constants variable for comparing by using If or Switch statement. So let's see enum as If Statement, we have Country enum that contains 3 fields:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rpek.enumdemo.enam; | |
/** | |
* Created by r.pek on 02.10.2017. | |
*/ | |
public enum Country { | |
PHNOM_PENH, | |
KOREA, | |
JAPAN | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Initilize value with JAPAN | |
//So It will print JAPAN on textview | |
Country countryType = JAPAN; | |
//If statement | |
if(countryType.equals(Country.PHNOM_PENH)){ | |
tvResult.setText(PHNOM_PENH+""); | |
}else if(countryType.equals(Country.JAPAN)){ | |
tvResult.setText(JAPAN+""); | |
}else if(countryType.equals(Country.KOREA)){ | |
tvResult.setText(KOREA+""); | |
} |
Here is a sample, I had initialized it value "JAPAN". So our enum will run and check each value (PHNOM_PENH, JAPAN, KOREA). JAPAN's case will be true so it will set TextView to JAPAN on the screen.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Initilize value with JAPAN | |
//So It will print JAPAN on textview | |
Country countryType = JAPAN; | |
//reference to xml to display result from java code | |
tvResult= (TextView) findViewById(R.id.tvResult); | |
switch (countryType){ | |
case PHNOM_PENH: | |
tvResult.setText(PHNOM_PENH+""); | |
break; | |
case JAPAN: | |
tvResult.setText(JAPAN+""); | |
break; | |
case KOREA: | |
tvResult.setText(KOREA+""); | |
break; | |
} |
4. Enum Field
We have 3 constructors that have each default value, if you use like this, it will return the default value, it has fruit field:
Fruit.APPLE.getFruit()// LIKE Fruit.PINE_APPLE.getFruit()//DON'T LIKE Fruit.BANANA.getFruit()//NORMAL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//we can user setter & getter with enum | |
Fruit.APPLE.setFruit("YES, Love APPLE"); | |
Fruit.PINE_APPLE.setFruit("NO, DON'T LIKE"); | |
Fruit.BANANA.setFruit("NORMAL BANANA"); | |
tv.setText(Fruit.APPLE.getFruit()+""); |
5. Enum Method
For enum method you can set/get the value from fruit fields like below:
6. Enum Iteration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//How to do enum iteration | |
StringBuilder concateString=new StringBuilder(); | |
for(Country country: Country.values()){ | |
concateString.append(country+ "\n"); | |
} | |
tvResult.setText(concateString); |
You want to download this project from Github here! Thank You!
This is an awesome post.Really very informative and creative contents.
ReplyDeleteIELTS Coaching in Chennai
Best IELTS Coaching in Chennai
French Language Classes in Chennai
pearson vue exam centers in chennai
Informatica MDM Training in Chennai
Hadoop Admin Training in Chennai
content writing course in chennai
IELTS Coaching in Tambaram
IELTS Coaching in Anna Nagar
I found that site very usefull and this survey is very cirious, I ' ve never seen a blog that demand a survey for this actions, very curious... Kopar at Newton Condo
ReplyDeleteThis is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog.. XE88
ReplyDeleteIt's late finding this act. At least, it's a thing to be familiar with that there are such events exist. I agree with your Blog and I will be back to inspect it more in the future so please keep up your act. TubeMate APK Review
ReplyDelete