• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"this"

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class cast1
{
int x=29;
public static void main(String args[])
{
int x=234;
//byte ch2 = (byte)i2;
this.x=23;
System.out.println(args[2]);
System.out.println("x=" + x + "this.x"+ this.x);
}
}

15)the above program on compilation gives " undefined variable this"
can anyone tell me what all are the places where "this" can be used
I know why it is used in constructor but sometimes it is used in methods also
so tell me what are the restrictions for it wwhen w use it in merthod
can it be used in static methods,main methods...
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
U can use This keyword inside the non static method's not the static one.U can also use this and super inside the instance initializer not inside the static initializer.U can use the this keyword inside the constructor but that has to be on the first line of the constrcutor,this and super cannot occur in the same method ,i.e either this or super can be used inside the constructor not both together.
 
nishesh chouhan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"this" and super can be used in constructor togther
but then super shud be the first word followed by "this"
correct me if I am wrong
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use one OR the other, not both.
You get one shot on the first line of the constuctor. You need to design your classes to handle that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic