Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static variables

 
prarthana reddy
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
public class Test
{
static int staticTotal = 10;
int nonStaticTotal = 20;

static void method1()
{
staticTotal = 30; //1
Test.staticTotal = 30; //2
new Test().staticTotal = 30; //3
}

void method2()
{
staticTotal = 30; //4
this.staticTotal = 30; //5
Test.staticTotal = 30; //6
new Test().staticTotal = 30; //7
}

public static void main(String[] args)
{ Test.method1();
System.out.println(staticTotal);
}

}
In the above code, in line 5 we are acceisng a static variable by using this. Is this correct ? because i thought static variables are class variables and they are not associated with any particular instance . So using this is not appropriate here. But the above code is compiling fine . How ? Again when i try to access a static method usig this ..its not compiling..can any one tell me the reason why this() is possible with static variables but not with methods?
 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using this.staticTotal from a non-static context.
So it's still allowed.

But you can not use this from a static context (i.e. from a static method).
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>Again when i try to access a static method usig this ..its not compiling..

Can you show how you tried? You can access the static methods the same way as the static variable.

This is how I did and it compiles fine.
---
public void method2() //changed this to public so can be called from main
{
staticTotal = 30; //4
this.staticTotal = 30; //5
Test.staticTotal = 30; //6
new Test().staticTotal = 30; //7
this.method1(); //calling the static method thro' this.
}

public static void main(String[] args)
{
Test.method1();
System.out.println(staticTotal);

Test t = new Test();
t.method2(); // which in turn will call method1 thro' this
System.out.println(staticTotal);
}
-----
 
Gayathri Ramanujam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Explanation of static variable...a very exp trainer told me this

stack has local variables.
exern stores global variable,static/constants
heap stores all objects.

Static variables are not associated with any instance of the class....so they cannot access any instance variables/variables defined in a class...since an instance might not have been created...

However an instance can access the static variables because they are already there and the JVM knows about them already...

-----------------------------------------
Heap --->Memory allocation at run time
-------------------------------------------
Extern---->Static variables live here...

-----------------------------------------

I hope I have explained this clearly.
 
prarthana reddy
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou one and all..am cleared of my doubt.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic