Query about static variables and this keyword?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • puneetsardana88
    New Member
    • Aug 2009
    • 57

    Query about static variables and this keyword?

    Code:
    class abc1
     {
    	public void abc2(abc my)
    	 {
    		this=my;
    	 }
     }
    class abc3
     {
    	public static void main(String args[])
    	 {
    		abc1 a= new abc1();
    		abc1 b=new abc1();
    		b.abc2(b);
    	 }
     }
    It gives error cannot assign a value to final variable this. Why this has been made as final in Java?


    Q2) Why we cannot have static members inside static functions in Java. Whats the problem and whats the benefit?


    Thank You in advance for taking your time n resolving my query.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You can't change "this". It's a reference to the currently executing object. If you could change it to something silly like null then you will have pulled the rug under the JVM's feet.

    Comment

    • puneetsardana88
      New Member
      • Aug 2009
      • 57

      #3
      Q2) Why we cannot have static members inside static functions in Java. Whats the problem and whats the benefit?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        It's not just static functions. You cannot have static members in a method because it doesn't make any sense to have them. Read the introduction to java tutorials to understand the meaning of static. You will then realize why it doesn't make any sense.

        Comment

        • puneetsardana88
          New Member
          • Aug 2009
          • 57

          #5
          Thanks

          Thanks I got the reason

          Comment

          Working...