scope concept on java?????

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

    scope concept on java?????

    i have code like this .. see carefully

    void test
    {
    int i;
    {
    int i;
    }
    }
    why does not java support this .... as C and C++ supports this

    plz help me out .... thanxxxxx
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by dmjpro
    i have code like this .. see carefully



    why does not java support this .... as C and C++ supports this

    plz help me out .... thanxxxxx
    They are indistinguishab le
    What then would be the output of calling

    Code:
     
    void test {
     int i = 5;
     {
     int i = 10;
     System.out.println(i + i);
     }
    }
    ?

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      i know it is not possible .....

      in C and C++ there is a way to access the outer varibale using ::(scope resultion operator)

      why this is stopped in java?????

      thanxxxxxx

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by dmjpro
        i know it is not possible .....

        in C and C++ there is a way to access the outer varibale using ::(scope resultion operator)

        why this is stopped in java?????

        thanxxxxxx
        Even in c++ that wont work dj. You use the scope resolution operator when you have the context.

        in
        Code:
         void test { 
         int i;
        }
        the context of i is the function test.
        in
        Code:
         void test{ 
         int i;
        {
        int y;
        }
        }
        The contect of y is still the function test. The {} cannot be named and so you cannot use :: with it. The scope of y is still the function test.
        so having
        Code:
         void test{ 
         int i;
        {
        int i;
        }
        }
        wont work because both have the same context i.e the function test and so you have a duplicate variable name

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          can't i access in C++ using TC++-3.0


          void test()
          {
          int i;
          {
          printf("%d",::i ); //outer i
          printf("%d",i); //inner i
          }
          }

          m i right ..... ??????

          thanx

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by dmjpro
            can't i access in C++ using TC++-3.0


            void test()
            {
            int i;
            {
            printf("%d",::i ); //outer i
            printf("%d",i); //inner i
            }
            }

            m i right ..... ??????

            thanx
            I thought :: was for global namespace. Anyway that does not compile on my VS6.0 and you missed the declaration for the inner i that you had earlier.



            P.S maybe I should copy this to the C++ forum.

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              sorry it was my mistake .....


              ok okkk again i am verryyy sorrryyyyy..... ....

              Comment

              • RedSon
                Recognized Expert Expert
                • Jan 2007
                • 4980

                #8
                You're right r0. No need to copy this to c/c++

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by dmjpro
                  sorry it was my mistake .....


                  ok okkk again i am verryyy sorrryyyyy..... ....

                  Hey DJ, you didn't do anything wrong. You were just asking and that's what the forum is for. We both learn that way.

                  Comment

                  • dmjpro
                    Top Contributor
                    • Jan 2007
                    • 2476

                    #10
                    ok thanxxxxx

                    Comment

                    Working...