• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

this Keyword for Statics??

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

I want to knwo if the keyword this should not be used from within a static block or just we cant use the this keyword to refer to statics. How come the following question has a,b, e as answers?

Given the following code, which statements can be inserted at the line indicated without compilation error??



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;

Please help me guys.

Regards,
Jothi Shankar Kumar. S
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I want to knwo if the keyword this should not be used from within a static block or just we cant use the this keyword to refer to statics. How come the following question has a,b, e as answers?



For the first part of the question... The "this" keyword can't be used in static methods. Static methods do not have any scope of the instance being referred to.

For the second part... Although, confusing, it is possible to use the "this" keyword (when "this" is in scope) to access static members.

Henry
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hnery,

Your quote,


For the second part... Although, confusing, it is possible to use the "this" keyword (when "this" is in scope) to access static members.



Then what it means to say "static means one copy per class". when we say that it is possible to use this keyword to statics, aren't we violating this rule of one per class???

Regards,
Jothi Shankar Kumar. S
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I do agree with Jothi,if it is possible to use to access the static members then aren't we violating the rules?
I have a another query,why this = new ThisUsage();is invalid for the answer?
 
Henry Wong
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
Hi Hnery,

Then what it means to say "static means one copy per class". when we say that it is possible to use this keyword to statics, aren't we violating this rule of one per class???

Regards,
Jothi Shankar Kumar. S



When you access a "static" field via the "this" object, it doesn't mean that each instance of the object has another copy. The compiler only uses the "this", during compilation, to figure out that you mean a member variable (versus a local variable).

In fact, for object references, it doesn't even have to be a valid object. This will actually work.



Henry
[ November 05, 2006: Message edited by: Henry Wong ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jothi,

The "this" keyword is a reference to the currently executing object. Also, according to K&B on p. 145, the Java language allows you to use an object reference variable to access a static member. If you combine these two facts, then you can use the "this" keyword to access static members of the currently executing object. By the way, this is just a syntax "trick". I believe that the compiler changes the syntax "this.staticMember" to "ClassName.staticMember".

I think that option c: is wrong, because you cannot assign another object to "this". "this" is not a regular reference, but is limited to referring to only the currently executing object. Option d is wrong, because "i" is a local variable and therefore out of scope of an object reference.

Regards,

Roger

[ November 05, 2006: Message edited by: Roger van't Hul ]
[ November 05, 2006: Message edited by: Roger van't Hul ]
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree Roger,

Should we consider this refernce as a final which can not be assigned to other object refernce of its type?
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roger,

Thanks for the reply.
 
Roger van't Hul
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sanjeev,

You asked if "this" could be interpreted as a final variable. To be honest, I don't know. I looked at the Java Language Specification and couldn't find any information there to answer this question.

I believe that "this", as I said before, isn't an ordinary reference. I don't think you should think of it in terms of a variable at all (and hence, you shouldn't think of it as a final variable). But rather, as a special construction which gives you the opportunity to reference an object from within itself.

Regards,

Roger
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roger van't Hul posted November 05, 2006 06:57 AM

You asked if "this" could be interpreted as a final variable. To be honest, I don't know.


At least the compiler says it is final:

The same would be if I said this=null;


Yours,
Bu.
 
Roger van't Hul
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Burkhard for clearing this up.

Regards,

Roger
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that every variable of a given class has a final reference 'this' that's initialized when the object is created. Just like every array object has its length property.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bu,

gives (jdk1.5)


So what Rogers fails to find in JLS,you got in javac
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Can anyone summarize it...based on using this operator for statics and instance variables, static instances....

That would be of great help.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Main {public static void main(String[] args) {System.out.println(1);}void foo(){this=new Main();}}
Hi all,
The code that is given here gives out error that "value cannot be assigned to final variable.But see this code once
public class Main{
Main ob;
public static void main(String[] args)
{
System.out.println(1);
}
void foo(Main ob)
{
this.ob=new Main();
}

}
Here "this" is used to make global variables to be overriden by local variables thats it.And as per my knowledge it is keywrd rather than a variable.
Regards,
Lalitha
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic