This discussion is archived
2 Replies Latest reply: May 15, 2013 11:14 PM by user11436730 RSS

Need help to understand the following code

user11436730 Newbie
Currently Being Moderated
I was visiting and trying to understand the code for java.util.Vector class , elements() methods.

Under the nextElements() method for inner class, i found synchronized(Vector.this) which was first time find for me.
Can any one help to explain this ?
public Enumeration<E> elements() {
     return new Enumeration<E>() {
         int count = 0;

         public boolean hasMoreElements() {
          return count < elementCount;
         }

         public E nextElement() {
          synchronized (Vector.this) {
              if (count < elementCount) {
               return (E)elementData[count++];
              }
          }
          throw new NoSuchElementException("Vector Enumeration");
         }
     };
    }
Thanks in advance.
  • 1. Re: Need help to understand the following code
    gimbal2 Guru
    Currently Being Moderated
    I assume you refer to the "Vector.this" bit. That is the way to refer to the "outer" class instance from a (non-static) inner class instance.

    That's way too vague, lets sketch a problematic and absolutely-not-to-copy-from example.
    public class MyOuterClass {
     
      private int test;
    
      private class MyInnerClass {
    
        private int test;
    
        public void doSomething(){
           test = 5;
        }
      }
    }
    In this example both the outer and inner class have a property 'test'. The assignment goes to the test of the inner class. Now what if you'd want to assign a value to test of MyOuterClass? You'd use the trick you now know:
    MyOuterClass.this.count = 10;
    Note: typed from memory without testing, excuse me if I made a mistake.
  • 2. Re: Need help to understand the following code
    user11436730 Newbie
    Currently Being Moderated
    Thanks gimbal2 for the help,I think I got the point now.
    As I understand from the example you have stated above, it to accomodate the access of the outerclass current instance from the innerclass instance for any required operation. Following is the example that I have stated to understand , probably it seems assuring the understating.
    public class OuterClass {
         private int count = 0;
    
         private class InnerClass {
    
              private int count =0 ;
              public void setCount(int count) {
                   this.count = count;
                   OuterClass.this.count = this.count * 2;
              }
              public void printCount() {
                   System.out.println("Inner Count : " + count);
              }
    
         }
    
         public void setCount(int count) {
              this.count = count;
         }
         public int getCount() {
              return this.count;
         }
         public void printCount() {
              System.out.println("Outer Count : " + count);
         }
    
    
         public static void main(String[] args) {
    
    
              OuterClass c = new OuterClass();
              c.count = 10;
              c.printCount();
              OuterClass.InnerClass i = c.new InnerClass();
    //          i.count = 20;
              i.setCount(25);
              i.printCount();
              c.printCount();
    
    
    
         }
    }
    Thanks.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points