Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access to static variable

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 289
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the point.
Thanks to all of you.
regs
vivek
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ajith,
that was upto the point.
vivek
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic