aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Access to static variable" Watch "Access to static variable" New topic
Author

Access to static variable

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
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");
System.out.println(this);
int temp = this.total; //confusing for me
if (temp > 5) {
System.out.println(temp);
}
}
}
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� ?

vivek
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
viveksri,
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.
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Anonymous
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.
vivek
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Vivek,
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.
<PRE>
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
testObj.someStaticMethod();

// Calling through the class name.
// works because the method is bound to the class
// Remember Math.min() ?? Think on the same lines.
StaticAccessDemo.someStaticMethod() ;
}
}
</PRE>
Hope this gives you something to start with.
Ajith
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 = this.total;
int temp = total;
(the `this' reference is unneccessary in the example given)
or
int temp = Test.total;
(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
Viveskri,
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 = this.total, 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.
Herbert.
Anonymous
Ranch Hand

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

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