wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static variables" Watch "Static variables" New topic
Author

Static variables

prarthana reddy
Ranch Hand

Joined: Jul 24, 2006
Posts: 48
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

Joined: Jul 04, 2006
Posts: 229
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

Joined: Aug 13, 2006
Posts: 178
>>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

Joined: Aug 14, 2006
Posts: 4
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

Joined: Jul 24, 2006
Posts: 48
Thankyou one and all..am cleared of my doubt.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static variables
 
Similar Threads
Method Local Inner class cannot access Final local Variables Problem
Static and non static variables
static
Member Variable
Qn on Overriding concept.