Pradeep Vairamani
Pradeep Vairamani

Reputation: 4302

Why can't we use 'this' keyword in a static method

class Sub {
    static int y;
    public static void foo() {
         this.y = 10;
    }
}

I understand that this represents the object invoking the method and that static methods are not bound to any object. But in the above mentioned case, the variable y is also static.

If we can invoke static method on class object, why can't we allow static methods to set the static variables of the class.

What is the purpose of this additional constraint?

Upvotes: 51

Views: 104805

Answers (11)

Md Zunaid Akhtar
Md Zunaid Akhtar

Reputation: 1

'this' keyword is used to store address /reference of object. For static method only one time memory is allocated, but in case of non static memory allocated each time when object is created each object has different address. So to access each object uniquely we need address and this keyword help us to do so. For static method only one copy is present, means only one reference, we can access static member without object reference.

Upvotes: 0

Siddharth Mittal
Siddharth Mittal

Reputation: 119

Let's Understand by a Simple Example

Example 1

public class Sample{
   static int num = 50;
   public static void demo(){
      System.out.println("Contents of the static method");
   }
   public static void main(String args[]){
      Sample.demo();
   }
}

Output

Contents of the static method

Example 2

public class Sample{
   static int num = 50;
   public static void demo(){
      System.out.println("Contents of the static method"+this.num);
   }
   public static void main(String args[]){
      Sample.demo();
   }
}

Output

error: non-static variable this cannot be referenced from a static context

Summary Observe Example 2 wisely , you will get to know that you will get CTE. These both examples will help you understand the difference

Keep Learning , Keep Growing

Upvotes: 0

ayush prashar
ayush prashar

Reputation: 466

There is no problem with static methods setting values for static fields.

The only issue is usage of this keyword. Please note that since static methods are processed at the time of class loading, it's all but certain that no "this" exists at the point of time, which is why its only logical the usage of this keyword isn't allowed in a static context.

On the other hand, static method can be invoked from an object because it is made accessible to the object. The intention behind static data members and behaviours is to make it common to all the instances of that class.

Upvotes: 1

anjli rajput
anjli rajput

Reputation: 1

when we declare variable and method is static then this is share by among object where this keyword only pointing to current object. suppose you have created five object of class foo then only one copy of made of (int y) shred by all object.so if you access int y using this keyword then compiler get a ambiguity which object have to point because static int y is shared by all object . you have access static variable using class name.

Upvotes: 0

Rajat Saxena
Rajat Saxena

Reputation: 11

"this" keyword is only applicable where an instance of an object is created. And in static method no instance is created because static method belongs to class area.

Upvotes: 1

mrswadge
mrswadge

Reputation: 1749

this is referring to this instance of the object Sub. As the method is static, there is not an instance of Sub.

Upvotes: 7

Sachindra N. Pandey
Sachindra N. Pandey

Reputation: 1252

The main reason why we can not use "this" in static method context:-

this :- "this" means current class OBJECT , so its clear that "this" only come in the picture once we intended to create an Object of that class.

static method:- there is no need to create an object in order to use static method. means "instance" or object creation doesn't any sense with "static" as per Java rule.

So There would be contradiction,if we use both together(static and this) . That is the reason we can not use "this" in static method.

Upvotes: 7

brimborium
brimborium

Reputation: 9512

Because this refers to the object instance. There is no object instance in a call of a static method. But of course you can access your static field (only the static ones!). Just use

class Sub {
    static int y;
    public static void foo() {
         y = 10;
    }
}

If you want to make sure you get the static field y and not some local variable with the same name, use the class name to specify:

class Sub {
    static int y;
    public static void foo(int y) {
         Sub.y = y;
    }
}

Upvotes: 101

sakthisundar
sakthisundar

Reputation: 3288

Keyword "this" refers to the object that you are operation with. In your case this inside any non-static methods or constructor (if you have one and and if you use "this" inside that), then "this" refers to that particular instance of the class Sub.So it is applicable only when the object is created. But anything in the static context of a class, you can use without even creating object for that as it is resolved during the class loading. "this" resolved only when object is created ( you can even say dynamically for which object). So "this" make sense in static context. Hope it helps. God bless.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533472

This means "this" object but there isn't one. In your case you can use the class name as @tibtof suggests.

Upvotes: 3

tibtof
tibtof

Reputation: 7957

To make your code work write it like this:

class Sub {
    static int y;
    public static void foo() {
         Sub.y = 10;
    }
}

You can set static fields in static methods, but you don't have access to this in static method because this represents the current instance of the object, and in a static method you have no instance.

Upvotes: 2

Related Questions