• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Static variables/method and This/super keyword

 
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read this in Khalid Mughal e-book.

The golden rule is that static code cannot access non-static members by their simple names. Static code is not executed in the context of an object, therefore the refrences this and super are not available.

Now, This is one of the Review Questions..

Giving the folowwing code, which statements can be placed at the indicated position without causing compilation errors?

public class ThisUsage{
int planets;
static int suns;
public void gaze(){
int i;
//....insert statements here.....

}//end of gaze

public static void main (String [] args){;}

}//end of class

Select the 3 correct answers:

a. i = this.planets;
b. i = this.suns;
c. this = new ThisUsage();
d. this.i = 4;
e. this.suns = planets;

Except a,b and e others got compile time error. I thought that 'this' keyword cannot be used for static variables and methods. Can anyone please elaborate on it.

Also, is 'this = new ThisUsage()' statements can be a legal statement (not in this particular example or code)
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To my knowledge we can refer to static variables using this keyword,but that is not a good one to do.

If there is any other reason? Let me also know that
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The keyword this cannot be used in the initializer for a static variable nor in a static initializer block nor in a static method.

What you are doing with e is accessing a static variable by using a reference to an object.

In that case all that matters is the type of the reference.

The static variable in the class type of the reference will be accessed.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
d. this.i = 4 won't work since this.i refers to the instance varialble of class ThisUsage. There is no 'i' defined.

Would somebody help to explain why option c. this = new ThisUsage();
is wrong.

Eleanor
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from the Java Language Specification.

The keyword this may be used only in the body of an instance method, instance initializer or constructor, or in the initializer of an instance variable of a class. If it appears anywhere else, a compile-time error occurs.
 
Eleanor Leong
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compiler gives error
cannot assign a value to final variable this
this = new ThisUsage();

Looks like variable 'this' is final and explains why option c is invalid.

Eleanor
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JLS specifically forbids the use of this in that statement.

It really wouldn't make sense for a statement like

this = ...

to be used because this is a reference to the current instance of the class.
 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all.

this.i is defined under the class member method gaze. Does it mean that this keyword can only refer/modify using a reference to class variable and not a local variable.

Kindly elaborate.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic