• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Static variable roles!

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello to everyone.
Usually the static variable that we never to use "this" pointer concepts.
Is it true?whereas i saw one programme where static variable used this pointer.
Even as i've written the same code.
-----------------
public class Staticthis {
public static int i = 22;
public void drive() {
static int i = 10;

System.out.println(this.i);

}


public static void main(String argv[]) {
int i = 99;
Staticthis h = new Staticthis();
h.drive();
}

}
anyone can you explain this code.
regards
rex
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rex,

You cannot used 'static' modifier for a method's local variable. For them only 'final' modifier is permitted.
Hence you will get a compiler error at line:
static int i = 10;

If we remove the static modifier and then run the program, the o/p is 22.
However if we remove 'this' keyword sysout, the o/p is 10.

I too am not sure on the reason for this. Might be related to shadowing of variables.
Other member(s)/moderator(s), please respond with the solution.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'this' actually returns the current object.

but as far as static is concern it means it belongs to the class. so it doesn't matter you use 'this' with static or not.

It will always remain same for all other objects.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ravi!!

you have said that

but as far as static is concern it means it belongs to the class. so it doesn't matter you use 'this' with static or not.

what it actualy means ,please correct if i am wrong



i am not sure with above ,please explain............

Thanks in advance!!!

Preparing SCJP 1.5
 
ravin kacha
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no the value will be 10, because when you declare the variable i in your method, which is local to that method only, sysout will find the local one first.
cause you have override the variable i, which is unfortunately having the same name as your static variable.

now you will be having another question that static members can not be override.....
yes, that is true static members can't be override but here it's been redefined and not override.
 
Lalit Bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravin,

Understood your logic. But what exactly is the reason that on using 'this' it goes for the static variable and not local?

Had the static 'i' been an instance variable, then the logic works well. But in case of static 'this' doesn't have a meaning.

Pls explain in detail.

Thanks in advance.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Ravin,

thanks for the explanation now
i am clear with the doubt but still
want to conform one thing please let me know
if i am right or not?

Does the above concept is related to Shadowing of
variables since both instance and local variable
has got the same name?
 
ravin kacha
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Lalit:

actually using this with any member is like below example


in the above example when you assign a value to j either with object 'a' or with 'this' it doesn't have to do anything with the instance. because it is a class variable.

it just a way to confuse you by assigning it a value with the instance.

see all the things are one and same for a static variable which i have described below:

a.j = 20;
this.j = 20;
A.j = 20; // you can reference a static member with its class name

all the statements above are one and same.

@Dhawani:

yes dhawani you are right, it is the concept of shadowing hope the above example will give you a better picture of static concepts.
[ August 30, 2007: Message edited by: ravin kacha ]
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ravin !!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic