8
class Demo {
   void show() {
      System.out.println("i am in show method of super class");
   }
}
class Flavor1Demo {

   //  An anonymous class with Demo as base class
   static Demo d = new Demo() {
       void show() {
           super.show();
           System.out.println("i am in Flavor1Demo class");
       }
   };
   public static void main(String[] args){
       d.show();
   }
}

In the above code, I do not understand the use of creating object d of Demo class with static keyword preceding it. If I eliminate the static keyword, it shows an error. Actually, I was going through anonymous inner class concept and got stuck here. Need help.... Can anyone please explain it?

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Ajay Khetan
  • 445
  • 1
  • 4
  • 11
  • That is the context you are in. the main methods is a static methods meaning there is no instance of `Flavor1Demo`. If you set `d` as non-static, it will only exist in an instance of `Flavor1Demo` so it can't be access from the `main` unless you build a instance first (new `Flavor1Demo().d.show();` – AxelH Jan 12 '17 at 10:57
  • How you directly access to d, it must be Flavor1Demo.d – Ashish Kamble Feb 25 '20 at 06:07

6 Answers6

12

The static keyword in Java means that the variable or function is shared between all instances of that class, not the actual objects themselves.

In your case, you try to access a resource in a static method,

public static void main(String[] args)

Thus anything we access here without creating an instance of the class Flavor1Demo has to be a static resource.

If you want to remove the static keyword from Demo class, your code should look like:

class Flavor1Demo {

// An anonymous class with Demo as base class
Demo d = new Demo() {
    void show() {
        super.show();
        System.out.println("i am in Flavor1Demo class");
    }
};

public static void main(String[] args) {

    Flavor1Demo flavor1Demo =  new Flavor1Demo();
    flavor1Demo.d.show();
}
}

Here you see, we have created an instance of Flavor1Demo and then get the non-static resource d The above code wont complain of compilation errors.

Hope it helps!

  • 1
    thanks a lot. so a static method can access variable/instance variable which are only static. Am i correct? – Ajay Khetan Jan 12 '17 at 11:06
  • _The static keyword in Java means that the variable or function is shared between all instances of that class, not the actual objects themselves._ Thats means that the variable/methods are part of the class, not shared between instances, there is no copy or anything done here. This is accessible from the Class like `Flavor1Demo.d` meaning that here is no need of an instance. – AxelH Jan 12 '17 at 11:10
  • 1
    A static method can only access static variables, because static methods belong to the class and not any object and thus they can only work with variables which belong to the class level and not to any object(that means static variables). – Sourav Purakayastha Jan 12 '17 at 11:18
3

You get an error by removing static keyword from static Demo d = new Demo() because you are using that object d of class Demo in main method which is static. When you remove static keyword from static Demo d = new Demo(), you are making object d of your Demo class non-static and non-staticobject cannot be referenced from a static context.

If you remove d.show(); from main method and also remove static keyword from static Demo d = new Demo(), you won't get the error.

Now if you want to call the show method of Demo class, you would have to create an object of your Demo class inside main method.

public static void main(String[] args){
     Demo d = new Demo(); 
     d.show();
 }
Yousaf
  • 27,861
  • 6
  • 44
  • 69
1

Thats because you try to use d that belongs to object in static method.

You would then have to create that object in main method.

static belongs to class, not object itself.

I want to know the difference between static method and non-static method

Community
  • 1
  • 1
Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166
1

That depends on the context you are in.

The main(String[]) methods is a static methods.

To stay simple, that means it doesn't exist in an instance of Flavor1Demo, there is no this here. If you set d as non-static, it will only exist in an instance of Flavor1Demo so it can't be access from the main unless you build a instance first (new Flavor1Demo().d.show();

But a static variable will be link to the class an not an instance, Meaning you can access it from a static context. In you case, the main method.

AxelH
  • 14,325
  • 2
  • 25
  • 55
0

In order to access methods or variables in main class without creating object in it,here we are defining anonymous inner class where we create object of static type so that its directly accessed from main class without creating the object.

0

There is no such thing as a static object in Java. The variable that points to the object can be static, but the idea of an object being static has no meaning.

The purpose of a static variable or any other static type member is to attach the member to the type itself rather than to an instance of the type.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10