Enumeration or enums in java

Enumeration or enums :

Enumeration is just like a list which contains only constant type value. The items in the enumerated list are called enums.
Using enums, can help to reduce the bugs in from your code. The basic components of an enum are its constants.
Enums can be declared as their own class or as a class member while we can’t declare within any methods.

Example :
Here you will see enum declaration outside of a class.


package com.pkjavacode.com;

enum Fruit {
APPLE, MANGO, BANANA, ORANGE
}

public class EnumExample {

private Fruit name;

/**
* @auther pradeep
*/
public static void main(String[] args) {
// fruit name;
EnumExample fn = new EnumExample();
fn.name = Fruit.APPLE;
System.out.println(fn.name);
}
}

Output :
APPLE

Note : in above example enum is declared outside of a class so you can not declare with private or protected modifier
so it is must be public or default.

You can also see an enum declaration within a class.

Example :


package com.pkjavacode.com;

public class EnumExample {
enum Fruit {
APPLE, MANGO, BANANA, ORANGE
}

private Fruit name;

/**
* @auther pradeep
*/
public static void main(String[] args) {
// fruit name;
EnumExample fn = new EnumExample();
fn.name = Fruit.APPLE;
System.out.println(fn.name);
}
}

Output :
APPLE

Note : enums value can’t accept a String or int type.
Example :


enum Fruit {"apple", "mango", "banana"}

The above declaration of enum is not legal and if you will try to do that you will get syntax error by the compiler.

Declaring Constructors, Methods and Variables in an enum :

An enum is a kind of class so you can add constructor, method and variable in an enum just like you add these other
classes in java.

Example :


package com.pkjavacode.com;

enum Fruit {
APPLE(3), BANANA(12), MANGO(2);
// declare a constructor
Fruit(int number) {
this.number = number;
}

// declare an instance variable
private int number;

// declare a method
public int getNumber() {
return number;
}
}

public class EnumExample {

Fruit name;

/**
* @author pradeep
*/
public static void main(String[] args) {
// fruit name;
EnumExample fn = new EnumExample();
fn.name = Fruit.APPLE;
System.out.println("Number of apple are :" + fn.name.getNumber());
}
}

Output :
Number of apple are : 3

 

Advertisements

static keyword in java

static keyword can use with variables, methods, blocks and nested class and can save some memory.

static with variables :
If static used with variable then it will be a single copy in whole class.

example : In this example company name is declared as static variable.


package com.pkjavacode.com;

public class EmployeeDemo {

String name;
int salary;
static String company = "IBM";

public EmployeeDemo(String name, int salary) {
this.name = name;
this.salary = salary;
}

void getEmployeeDetails() {
System.out.print("Employee name :" + name + ", Salary :" + salary
+ ", Company :" + company);
System.out.println();

}

public static void main(String[] args) {
EmployeeDemo e1 = new EmployeeDemo("Pradeep Yadav", 25000);
EmployeeDemo e2 = new EmployeeDemo("Amit Verma", 35000);
e1.getEmployeeDetails();
e2.getEmployeeDetails();
}

}

Output :

Employee name :Pradeep Yadav, Salary :25000, Company :IBM
Employee name :Amit Verma, Salary :35000, Company :IBM

 

 

static with method :
If static keyword used with method then there is no need to create instance to call that method.

example :


package com.pkjavacode.com;

public class EmployeeDemo {
static void getEmployeeDetails() {
System.out.println("static method example");
}

public static void main(String[] args) {
EmployeeDemo.getEmployeeDetails();
}
}

Output :

static method example
static block :
If you want to print something before main() method execution and after class loaded.

example :


package com.pkjavacode.com;

public class EmployeeDemo {

public static void main(String[] args) {

}

static {
System.out.println("This is a static block message");
}
}

Output :

This is a static block message
static with inner class :

If you will used static keyword with inner class then there is no need to create Outer class object, just create inner class object in outer class main() method and can access their methods.

example :


package com.pkjavacode.com;

public class EmployeeDemo {

public static void main(String[] args) {
InnerEmp e = new InnerEmp();
e.display();
}

static class InnerEmp {
void display() {
System.out.println("This is a static inner class message");
}
}
}

Output :

This is a static inner class message