#why is there multiple copies of static class in java?

1 messages · Page 1 of 1 (latest)

tranquil wraith
#

The question is contained inside the comment in Main.java

Asd.java

public class Asd {

  private AsdInner asdg;
  
  public static abstract class AsdInner {

    public abstract void run();

  }

  public void setInner(AsdInner asdinner){

    asdg = asdinner;

  }

  public void call(){

    asdg.run();
    
  }

}

Main.java

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {
    
    Asd asd = new Asd();
    
    asd.setInner(new Asd.AsdInner() {
      //there is only 1 copy of Asd.AsdInner in memory,
      //I am setting that very single copy's run() method to
      //print out "asd"
      public void run() {

        System.out.println("asd");

      }

    });

    asd.call(); //output: asd

    Asd asd2 = new Asd();
    asd2.setInner(new Asd.AsdInner() {
      //there is only 1 copy of Asd.AsdInner in memory,
      //I am setting that very single copy's run() method to
      //print out "asd2"
      public void run() {

        System.out.println("asd2");

      }

    });

    asd2.call(); //output: asd2

    asd.call(); //output: asd, why is it not asd2?? it should be asd2 because I already set that very single copy's run method to be "asd2"

  }

}
fathom narwhalBOT
#

<@&987246399047479336> please have a look, thanks.

fathom narwhalBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

rain sun
#

static inner classes aren't single instance classes

#

they're just not tied to an instance of the outer class, unlike non-static inner classes

#

they can still access private static methods of the outer class though

tranquil wraith
#

so static keyword for members and static keyword for class mean completely different thing right?

haughty plinth
#

as samantha explained, that works the same way for nested classes

tranquil wraith
#

alright so like this
e.g
Parent{

StaticInnerClass{

}

}

So no matter how much I instantiate the parent class, there is only 1 single copy of StaticInnerClass right?

haughty plinth
#

yes

#

referring to a static thing via an object is a flaw btw and flagged by all ides

#

static things should be referred to by the class name

#

i. e. Outer.Inner

#

and not

#

someOuterInstance.Inner

tranquil wraith
#

alright but that StaticInnerClass is just like a normal class it can have members, functions, etc no difference than normal class. But it can only access it's parent static members only right?

haughty plinth
#

correct

#

anything else wouldn't make sense

#

its static. its decoupled from any instances of anything whatsoever

tranquil wraith
#

aight thanks alot for the explanation