a little confusion with my code .....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    a little confusion with my code .....

    here is my code ....... plz look at carefully

    class Test2
    {
    }

    class Test1
    {
    void method(Test2 i)
    {
    System.out.prin tln("Debasis: " + i);
    }
    }

    class AnonymousTest
    {
    public static void main()
    {
    Test1 t = new Test1();
    String str = "Debasis";
    t.method(new Test2(){ public String toString(){retu rn str;} });
    }
    }

    now i get a compiler error ... the variable str needs to be declared as final

    why this happens ... plz help....thanxxx xxx
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by dmjpro
    here is my code ....... plz look at carefully

    class Test2
    {
    }

    class Test1
    {
    void method(Test2 i)
    {
    System.out.prin tln("Debasis: " + i);
    }
    }

    class AnonymousTest
    {
    public static void main()
    {
    Test1 t = new Test1();
    String str = "Debasis";
    t.method(new Test2(){ public String toString(){retu rn str;} });
    }
    }

    now i get a compiler error ... the variable str needs to be declared as final

    why this happens ... plz help....thanxxx xxx
    1.) dj you forgot the code tags again!
    2.)You are accessing a local variable from an inner class therefore it must be declared final

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      again sorrry .....thanx for ur replyyyy

      i know i trying to access the local varibale ... so it needs to be declared as final

      but i want to know if the java compiler allows to do so then what would happen.....
      i just want to know what is the logic behind it ....

      plz help me .... thanxxxxx

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by dmjpro
        again sorrry .....thanx for ur replyyyy

        i know i trying to access the local varibale ... so it needs to be declared as final

        but i want to know if the java compiler allows to do so then what would happen.....
        i just want to know what is the logic behind it ....

        plz help me .... thanxxxxx
        It's a memory allocation issue dj. When memory for the inner class object is reserved, everything about it needs to be known at that moment so that the best fit slot is selected for it. That's why the variable allowed for it must be final.

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          it is not still clear to me ..... if u can't explain me in one thread ... plz send me a good link which can describe in details...

          plz help ..... thanx

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by dmjpro
            it is not still clear to me ..... if u can't explain me in one thread ... plz send me a good link which can describe in details...

            plz help ..... thanx
            Try this but you may need to read the JVM specification to fully understand this.

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              thanx a lot ... have a good day

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                hello frndsss ..... i got a good exlaination on it .... if can plz check it out



                written by hiwa from java world


                See: http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8. 1.3
                An inner class, however it is declared and defined in a particular method, makes a separate object outside the method so the object can't access method local variables, which are allocated on the mthod's stack frame, directly. Thereof that enforces Java to pass a copy of local variable to the inner class object.

                // str is a copy of local variable str!!!
                t.method(new Test2(){ public String toString(){retu rn str;} });

                If the method happens to modify the value of the local var, the new value doesn't reflect to the copy in the inner class object -- that should invite a terrible disaster. In order to prevent it, local var referenced in an inner class must be final.

                Comment

                Working...