File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes use of super,this  in static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "use of super,this  in static method" Watch "use of super,this  in static method" New topic
Author

use of super,this in static method

Aparanji Raju
Ranch Hand

Joined: Apr 02, 2000
Posts: 65
Hi,the following is a code on overriding static methods.While i understand that "this"cannot be used with static data,I would like to know why "super" is not invoking the super class's method.

class StaticOverride
{
static String st;
public static void statString()
{
st="SUPER CLASS STRING";
System.out.println("Super: "+st);
}
}
class StaticTest4 extends StaticOverride
{
static String message;
public static void statString()
{
message = "DERIVED CLASS STRING";
System.out.println("Derived: "+message);
}
public static void main(String arg[])
{
statString();//dynamic binding.....prints derived class's string.
//super.statString(); gettting an error:Undefined variable:super
//this.statString(); getting an error:Undefined variable:this
StaticOverride.statString();//getting superclass string
StaticTest4.statString();//getting derived class string.
}
}

Adrian Ferreira
Ranch Hand

Joined: May 29, 2000
Posts: 118
Aparanji,
As you can see, the correct way to call static method is "ClassName.staticMethodName()".
You can also call an static method like this:
ClassName obj = new ClssName(); //create object
obj.staticMethodName(); //call instance method
But it�s not recomended too becauso it could be miss assumed that the instance were modified by that method. In fact, just the static data can be modified by static methods and that line above acts as same as:
ClassName.staticMethodName();
Since "this" and "super" is applied only for class instances, it does not make sense to use them for static methods. IMO, not using them results in clearly and safer code.
Hope to help,
Adrian
Aparanji Raju
Ranch Hand

Joined: Apr 02, 2000
Posts: 65
Thanks Adrian.I had come accross the following statement regarding the use of "this" :
"The 'this' reference which is valid in instance methods can be used to access static entities too."
Is this statement legal?
Thanks
Aparanji
Suma Narayan
Ranch Hand

Joined: Apr 03, 2000
Posts: 136
Aparanji,
That's right. You can use this in a instance method to access static variables of its class. However, in a instance method, this is assumed even if you don't specify it. Please check the other forum posted by you with the same question, for details. I have explained it there.

In the above program, I have intentionally declared and initialized local variable j. This is to show you the use of this to access class varaible. In the absence of local varaiable with the same name as class variable, you need not use this as the compiler assumes this in this case. Also note that if you just declare the local variable and don't initialize, the compiler still attaches this to j. Once you initialize local variable, the compiler now knows that j implies the local varaiable j and not the class variable j. At this point, you differentiate by specifying this to the class variable.
I hope this helps you.
Suma


[This message has been edited by Suma Narayan (edited June 13, 2000).]
Adrian Ferreira
Ranch Hand

Joined: May 29, 2000
Posts: 118
Thanks Suma to show us how the compiles works.
I would like to point that using "this" to access static variables is not the better way to do that. It is more difficult when we have more than one instance that uses static class variables. In this case it could not be clear, during programming, where the variables is related to class or to instance.
In the code segment bellow, both "println" give the same result but the second one is more clear and, im my opinion, there is no good reason to use the first one.
public class TestThis{
static int j = 20;
public static void main(String args[]){
TestThis test = new TestThis();
test.amethod();
TestThis test2 = new TestThis();
test2.amethod();
}
public void amethod(){
TestThis.j++ ;
System.out.println("Value of static variable j is " + this.j);
System.out.println("Value of static variable j is " + TestThis.j);
}

}
And its prints:
Value of static variable j is 21
Value of static variable j is 21
Value of static variable j is 22
Value of static variable j is 22
We know that j is never relatad to instance, but it could be wrongly assumed.
But I agree that Suma�s code is important to understand java compiler rules.
Adrian
Suma Narayan
Ranch Hand

Joined: Apr 03, 2000
Posts: 136
Thanks Adrian.
I also believe that for accessing class varaiables, one should not use this from the non-static method. I do agree with you that it is not a better way of accessing and adds to confusion. The scenario you have mentioned is perfect. The best way to access a static variable or method is via the CLASS NAME.

I just wrote the program to show Aparanji that what he said is right.
Suma
[This message has been edited by Suma Narayan (edited June 13, 2000).]
Khurram Fakhar
Ranch Hand

Joined: May 29, 2000
Posts: 65
Hello.
Guyz one more thing , Static members and variables have the same impcact in program as GLOBAL in C / C++ and in any other evironment. so we can even directly call them any where in the class by jsut calling its name.
class StaticOverride
{
static String st;
public static void statString()
{
st="SUPER CLASS STRING";
System.out.println("Super: "+st);
}
public static void t() {
System.out.println("gotcha");
}
}
class StaticTest4 extends StaticOverride
{
static String message;
public static void statString()
{
message = "DERIVED CLASS STRING";
System.out.println("Derived: "+message);
}
public static void main(String arg[])
{
statString();//dynamic binding.....prints derived class's string.
StaticOverride.statString();//getting superclass string
StaticTest4.statString();//getting derived class string.
t();
}
}

Khurram Fakhar
Ranch Hand

Joined: May 29, 2000
Posts: 65
Hello.
Guyz one more thing , Static members and variables have the same impcact in program as GLOBAL in C / C++ and in any other evironment. so we can even directly call them any where in the class by jsut calling its name.
class StaticOverride
{
static String st;
public static void statString()
{
st="SUPER CLASS STRING";
System.out.println("Super: "+st);
}
public static void t() {
System.out.println("gotcha");
}
}
class StaticTest4 extends StaticOverride
{
static String message;
public static void statString()
{
message = "DERIVED CLASS STRING";
System.out.println("Derived: "+message);
}
public static void main(String arg[])
{
statString();//dynamic binding.....prints derived class's string.
StaticOverride.statString();//getting superclass string
StaticTest4.statString();//getting derived class string.
t();
}
}

Aparanji Raju
Ranch Hand

Joined: Apr 02, 2000
Posts: 65
Thanks Suma,Adrian and Khurram
Your explainations do clear up my doubt.Actually,I didnt comprehend the meaning of the statement I wrote until Suma said that this can be used from an instance method to access static entities.I guess I must concentrate more.
From Khurram's post:static has the same effect as global in c/c++.
Apart from giving the globalising effect and perhaps to maintain count of objects,does static have any other uses?
Thanks again.
Aparanji.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: use of super,this in static method