This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Access to static variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Access to static variable" Watch "Access to static variable" New topic

Access to static variable

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
This is from mock exam of John Hunt Q.19
public class Test {
static int total = 10;
public static void main (String args []) {
new Test();
public Test () {
System.out.println("In test");
int temp =; //confusing for me
if (temp > 5) {
A. The class will not compile
B. The compiler reports and error at line 2
C. The compiler reports an error at line 9
D. The value 10 is one of the elements printed to the standard output
E. The class compiles but generates a runtime error
According to them answer is D.
This is little bit confusing for me. Can we access static variable using �this� ?

Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
Instance methods can access static variables and methods. After all, they belong to the same class. But not the other way, ie., static methods cannot access instance variables or methods because they do not have the 'this' reference.
Hope this helps. I wish I had some time to write a small example program for you....but I have to run for an errand now. Think about it and come back here if you don't understand. We can talk about it more.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi ajith,
I understand what you are saying but I m confuse because
� As per my knowledge static variables/methods are not bound to any particular instance of object, and �this� refer to a particular instance of a object.�
So if they are not bound to particular instance of a object then how can be access them using �this� ?.
May be I m wrong. Please explain me.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
You are right. Static methods are not bound to any particular object, but are bound to the class. That does not mean objects cannot access it. The 'this' reference which is valid in instance methods can be used to access static entities too.
On the other hand, because there is no 'this' reference in static methods, they don't have any way to access instance entities.
Maha is so good in giving analogies. I tried hard, but couldn't come up with a good one . So I wrote this little program to help you understand the concept.
public class StaticAccessDemo
static String myString = "Hello" ;
static int staticIntVar = 10 ;

String instanceString = "I am the instance string";

static void someStaticMethod()

// Can access other static variables.
System.out.println( myString ) ;

// Cannot access instance variables because there is
// no 'this' reference. All of the following lines are illegal.
// instanceString = 2 ;
// instanceString = "Static in winter is annoying" ;
// someInstanceMethod() ;

void someInstanceMethod()
// 'this' reference is valid here.
// Can access both static instance methods/variables from here.

instanceString = "Inside method" ;
// is same as..
this.instanceString = "Inside method" ;

public static void main( String[] foobar )
// because main is static, you can directly access
// other static methods/variables without creating
// objects.
someStaticMethod() ;

// 'this' is not valid in static methods so the next
// line will not compile.
// this.someStaticMethod();

StaticAccessDemo testObj = new StaticAccessDemo() ;

// Different ways to access the same static method ..

// Calling through the object instance

// Calling through the class name.
// works because the method is bound to the class
// Remember Math.min() ?? Think on the same lines.
StaticAccessDemo.someStaticMethod() ;
Hope this gives you something to start with.
Greg Whelan
Ranch Hand

Joined: May 18, 2000
Posts: 52
Note that there are a couple other (equivalent) ways that this line could have been written:
int temp =;
int temp = total;
(the `this' reference is unneccessary in the example given)
int temp =;
(visible static members can be accessed through an instance of the class, or via the class name, as this line shows).
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
The facts hitherto given to you by Ajith et all not withstanding, you also need to realize/note that in the code given, the call int temp =, occurs in a constructor.The constructor is not a static method(in fact it is not a method at all), so in this particular case, "this" exists.
Just an add on.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I got the point.
Thanks to all of you.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks ajith,
that was upto the point.
I agree. Here's the link:
subject: Access to static variable
Similar Threads
q on
Using this to reference static variables
Questions from John Hunt mock
sample Q
John Hunt M.E Q17