File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes is it wrong to use Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "is it wrong to use "this" in static methods?" Watch "is it wrong to use "this" in static methods?" New topic

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

srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
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() {
public static void print() {
public static void main(String args []) {

please use the [code][/code] tags when showing code. visit <a href="" target="_blank" rel="nofollow"></a> ,for more details
Jyotsna Clarkin
Ranch Hand

Joined: Jan 26, 2001
Posts: 158
Yes, this is a reference to an instance of the class. Static methods cannot have this.
Hassan Naqvi
Ranch Hand

Joined: May 03, 2001
Posts: 158
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.
Hassan Naqvi.

Always Belive On Logic!!
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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


Deepak B

Joined: Jun 19, 2001
Posts: 17
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

Joined: Aug 30, 2000
Posts: 3141
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

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
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
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
// Invoke static method
System.out.println(Circle.bigger(c1, c2));
System.out.println(c1.bigger(c1, c2));
I agree. Here's the link:
subject: is it wrong to use "this" in static methods?
It's not a secret anymore!