Monday, April 23, 2018

How to use Java Enum with Android studio

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:

Fruit.APPLE.getFruit()// LIKE

Fruit.PINE_APPLE.getFruit()//DON'T LIKE

Fruit.BANANA.getFruit()//NORMAL


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!

4 comments:

  1. 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

    ReplyDelete
  2. This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog.. XE88

    ReplyDelete
  3. It'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