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?

2
  • 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(); Commented Jan 12, 2017 at 10:57
  • How you directly access to d, it must be Flavor1Demo.d Commented Feb 25, 2020 at 6:07

6 Answers 6

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!

Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot. so a static method can access variable/instance variable which are only static. Am i correct?
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.
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).
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();
 }

Comments

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

4 Comments

ohh. we can only use static variable/instance variable inside a static method. Am i correct?
@AjayKhetan no :) you can use it like Flavor1Demo.d basically everywhere.
that one is possible. But we cant directly use it.
@AjayKhetan thats because it is declared as static variable - it belongs to class not object. If its declared as non-static then it belongs to object and you first need to create one before you can access it: new Flavor1Demo().d
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.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.