Thursday, May 29, 2014

About Final

Know about basics of final , where to use and when compiler sees final keyword then how it reacts?


How final is used inside a method for local variables and as parameter of any methods ?


Can we use final with class definition? if yes how it will affect the relationship between the classes? can we use final with the instance methods, if yes then what the restriction it can impose?

ENUM - Part 2

What is correct syntax to write an enum in java?
package in.technoscience.first;

public enum Work {
Tough,Easy,VeryEasy;
}

1) Use the keyword enum which will tell that type being created is an enum.
2) Inside the curly braces, put in constant that you want to use in your application.

How compiler deal with the enum created?
First thing to remember is as it is a type in java so compiler will generate .class file with the name enum means here we will can find Work.class file.

The code generated by the compiler:
 // the enum user has defined is treated as class by compiler.
// compiler attach final with class so it means it can not be subclassed 
public final class Work extends Enum { //user defined enum extends java.lang.Enum
//enum value that user used in enum is considered as the constant of type Work(class name)
 public static final Work Tough;
 public static final Work Easy;
 public static final Work VeryEasy;
 private static final Work ENUM$VALUES[];// an array to store enum values
//compiler provide private constructor, another reason it can not be subclassed
 private Work(String s, int i) {
  super(s, i);
 }
 // values method is put by compiler which plays great role in iterating through enum
 public static Work[] values() {
  Work awork[];
  int i;
  Work awork1[];
  System.arraycopy(awork = ENUM$VALUES, 0,
    awork1 = new Work[i = awork.length], 0, i);
  return awork1;
 }

 public static Work valueOf(String s) {
  return (Work) Enum.valueOf(in / technoscience / first / Work, s);
 }
//a static block is used where each value is instantiated with enum value name and index no.
   static {
  Tough = new Work("Tough", 0); // it represents every enum value is an instance of user defined enum type
  Easy = new Work("Easy", 1);
  VeryEasy = new Work("VeryEasy", 2);
  ENUM$VALUES = (new Work[] { Tough, Easy, VeryEasy }); //anonymous array
 }
}

Here you can visualize , for 3 lines of code you have written, how compiler deals with that ! 

Can we write an enum inside a class?
yes we can write an enum inside a class also. 
package in.technoscience.first;
public class Uploader {

  public enum UploadStatus { INITIALIZING, IN_PROGRESS, COMPLETE };

  // Class body

}
Here compiler will generate two .class file
1st : Uploader.class 
2nd: Uploader$UploadStatus.class file

code generated by compiler :
ackage in.technoscience.first;

// user defined enum as inner class inside the class where it has been defined
public class Uploader
{
    public static final class UploadStatus extends Enum
    {

        public static final UploadStatus INITIALIZING;
        public static final UploadStatus IN_PROGRESS;
        public static final UploadStatus COMPLETE;
        private static final UploadStatus ENUM$VALUES[];

        public static UploadStatus[] values()
        {
            UploadStatus auploadstatus[];
            int i;
            UploadStatus auploadstatus1[];
            System.arraycopy(auploadstatus = ENUM$VALUES, 0, auploadstatus1 = new UploadStatus[i = auploadstatus.length], 0, i);
            return auploadstatus1;
        }

        public static UploadStatus valueOf(String s)
        {
            return (UploadStatus)Enum.valueOf(in/technoscience/first/Uploader$UploadStatus, s);
        }

        static 
        {
            INITIALIZING = new UploadStatus("INITIALIZING", 0);
            IN_PROGRESS = new UploadStatus("IN_PROGRESS", 1);
            COMPLETE = new UploadStatus("COMPLETE", 2);
            ENUM$VALUES = (new UploadStatus[] {
                INITIALIZING, IN_PROGRESS, COMPLETE
            });
        }

        private UploadStatus(String s, int i)
        {
            super(s, i);
        }
    }


    public Uploader()
    {
    }
}


Saturday, May 17, 2014

Enum - Part1

Some points about Enumerated type in java :

Enums

  • Enums were introduced as new type in java in jdk1.5. prior to that there was concept of classes and interfaces(as type) only. But these were not sufficient when we need to define a finite set of allowed values for a specific data type. So enumerated type concept was introduced and it was known by the name enum. 
  • Enums allow you to dump most of your "public static final" variable declarations.
  • Enums are type, like class : As a result, we get type-safety, compile-time checking, and the ability to use them in variable declarations. This overcome the problems that we have faced while using public static final integer or String constant.
  • Enums extend java.lang.Enum : java.lang.Enum was a new class in jdk1.5, and is not itself an enumerated type. All enumerated types implicitly extend Enum.
  • Enumerated types aren't integers :Each declared value is an instance of the enum class itself; this ensures type-safety and allows for even more compile-time checking.
  • Enums have no public constructor :This removes the ability to create additional instances of the enum not defined at compile-time. Only those instances defined by the enum are available. It has only protected constructor.
  • Enum values are public, static, and final :Values cannot be overwritten, changed, or otherwise messed with in ways that affect your programming logic. The enum itself is effectively final, as it cannot be subclassed.
  • Enum values can be compared with == or equals( ) :Because enums are effectively final, and there is a distinct set of values, you can use—for comparison. Additionally, enumerated types have a working equals( ), for use in collection classes.
  • Enums implements java.lang.Comparable :As a result, enum values can be compared with compareTo( ), and ordering occurs in the same order as values are declared in the enum declaration.
  • Enums override toString( ) :The toString( ) method on an enumerated type returns the name of the value.
  • Enums provide a valueOf( ) method :The static valueOf( ) method complements toString( ). If you change the behavior of toString( ), you need to also change the behavior of valueOf( ). These two methods should always be mirror images of each other.
  • Enums define a final instance method named ordinal( ) :oridinal( ) returns the integer position of each enumerated value, starting at zero, based on the declaration order in the enum. Most programmers will have no use for this method. It is designed for use by sophisticated enum-based data structures, such java.util.EnumSet and java.util.EnumMap
  • Enums define a values( ) method : values( ) allows for iteration over the values of an enum.