Date Archives July 24, 2016

Types of variables

local variables

  • Local variables are declared in methods, constructors, or blocks.
  • Local variables are visible only within the declared method, constructor or block.

Example…Screen Shot 2016-07-24 at 2.42.25 PM

instance variables

instance

  • Instance variables are declared in a class, but outside a method, constructor or any block.
  • The instance variables are visible for all methods, constructors and block in the class.

Example…Screen Shot 2016-07-24 at 2.43.22 PM

Class / Static variables

static

  • Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
  • Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class.
  • Static variables can be accessed by calling with the class nameClassName.VariableName.

Example…Screen Shot 2016-07-24 at 2.44.22 PM

 

public vs. protected vs. private

Screen Shot 2016-07-24 at 2.21.46 PM

  • public: A class, method, constructor, interface etc declared public can be accessed from any other class.
  • protected: Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members’ class.
  • no modifier: A variable or method declared without any access control modifier is available to any other class in the same package.
  • private: Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself.

Enumeration vs. Iterator

Comparison

Don’t be confused between Enum and Enumeration, totally different.

  • With both, you can enumerate(iterate) the elements in a collection of objects, but Enumeration is older, and Iterator is improved one.
  • Main difference between Enumeration and iterator is Iterator has a remove() method while Enumeration doesn’t.
  • Enumeration is used when ever we want to make Collection objects as Read-only.
  • Iterator is more secure and safe as compared to Enumeration because it does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException.

Enumeration

Screen Shot 2016-07-24 at 12.43.58 PM

Example…

public class EnumerationEx{
   public static void main(String[] args){
      Enumeration numEnumeration;
      Vector num = new Vector();
      num.add(100);
      num.add(200);
      num.add(300);

      numEnumeration = num.elements();
      while(numEnumeration.hasMoreElements()){
         System.out.println(numEnumeration.nextElement());
      }
   }
} 

Output…

100
200
300

Iterator

Screen Shot 2016-07-24 at 12.43.27 PM

Example…

public class IteratorEx{
   public static void main(String[] args){
      LinkedList<Integer> list = new LinkedList<Integer>();
      list.add(100);
      list.add(200);
      list.add(300);

      Iterator<Integer> iterator = list.iterator();

      while(iterator.hasNext()){
         Integer tmp = iterator.next();
         System.out.println("Before: "+tmp);
         if(tmp==200)iterator.remove();
      }

      iterator = list.iterator();
      while(iterator.hasNext()){
         System.out.println("After: "+iterator.next());
      }
   }
}

Output…

Before: 100
Before: 200
Before: 300
After: 100
After: 300