Connecting Tech Pros Worldwide Forums | Help | Site Map

Query about static variables and this keyword?

Newbie
 
Join Date: Aug 2009
Posts: 15
#1: 4 Weeks Ago
Expand|Select|Wrap|Line Numbers
  1. class abc1
  2.  {
  3.     public void abc2(abc my)
  4.      {
  5.         this=my;
  6.      }
  7.  }
  8. class abc3
  9.  {
  10.     public static void main(String args[])
  11.      {
  12.         abc1 a= new abc1();
  13.         abc1 b=new abc1();
  14.         b.abc2(b);
  15.      }
  16.  }
  17.  
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.



Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: 4 Weeks Ago

re: Query about static variables and this keyword?


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.
Newbie
 
Join Date: Aug 2009
Posts: 15
#3: 3 Weeks Ago

re: Query about static variables and this keyword?


Q2) Why we cannot have static members inside static functions in Java. Whats the problem and whats the benefit?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: 3 Weeks Ago

re: Query about static variables and this keyword?


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.
Newbie
 
Join Date: Aug 2009
Posts: 15
#5: 3 Weeks Ago

re: Query about static variables and this keyword?


Thanks I got the reason
Reply