Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is it wrong to use "this" in static methods?

 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it wrong to use this in static methods?
when i execute this ,it gives me a compi err,
can anybody pls expl this
public class Test1 {
public static void test() {
this.print();
}
public static void print() {
System.out.println("Test");
}
public static void main(String args []) {
test();
}
}
 
Jyotsna Clarkin
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is a reference to an instance of the class. Static methods cannot have this.
 
Hassan Naqvi
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srinivas,
static methods are those methods which are not dependent of any object in order to execute.For example, we make static methods in those situations where we want the method to run before the object creats.The best example of static method is main().We make the main(), static because we wants the JVM(or you may say compiler) to enter in our programe for compilation.And ofcourse compiler searchs the main() to enter in a programe, this means that it will enter when there is no object is created.
Now if you use this in static method it would be sensless(compile time error).
Hope this will help.
Regards,
Hassan Naqvi.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srinivas
Let me try to combine what Jyotsna and Hassan said...
In a method the keyword 'this' refers to the instance of the class that was used in the method call. Here is your code with some additions...

In the two method calls to myMethod(), in the first one, in the actual body of the method, refering to 'this' will be refering to the object obj1. In the second call using this will refer to obj2. This is because myMethod is an instane method and automatically gets a reference to the current object.
In the static method, because it is a class method, there is no current object. You don't have to use an instance of the class to call that method - just like in your code. Even if you do use an object to call that method, it still doesn't get a reference to it.
I hope that helps you

Dave
 
Deepak B
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends
"this" keyword implicitly include with every object. Whenever many object of one class is created each object contain his own copy of variable, method, using �this� u can identify which method or which class variable is used ,Static variable doesn�t belong to any class ,static variable execute before class constructor is called, it is in depended keyword, That�s way u can�t bound �this� keyword with any class method or variable or object ,
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepak B,
Plese read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
you can invoke a static method by class name or instance name.
In this example there is an instance method and a static method
which do the same thing:
They compare circles by size. The instance method only needs
the comparison circle as parameter because the other circle is
being passed by reference (c1.bigger(c2));
The static method can also be invoked by reference (c1.bigger(c1,
c2) but it cannot acces the instance variables of the referenced
object (by "this") so it needs both circles beeing passed as
parameters.
--------------------------------------------------------------
class Circle {
public double x, y, r; //Instance Variables
Circle(int x, int y, int r) {this.x=x; this.y=y; this.r=r;} //Constructor
// Instance Method for comparing circles
// The instance method only needs one circle passed as the second circle
// is passed by reference and accessible by "this"
public Circle bigger(Circle c)
{
if (c.r > r) return c; else return this; // if (c.r > this.r) ...
// compiler inserts this.r automatically!
}
// Class Method for comparing circles
// The static method needs both circles as it cannot reference the
// instance variables by "this" like an istance method
public static Circle bigger(Circle a, Circle b)
{
if (a.r > b.r) return a; else return b;
}
public String toString() {return "Circle: x ="+x+"y= "+y+"r= "+r;}
}
public class Circletest {
public static void main (String args[])
{
Circle c1 = new Circle(2,3,2);
Circle c2 = new Circle(2,4,4);
// Invoke instance method
System.out.println(c1.bigger(c2));
// Invoke static method
System.out.println(Circle.bigger(c1, c2));
System.out.println(c1.bigger(c1, c2));
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic