•  dax   ( @dax@beehaw.org ) 
      link
      fedilink
      English
      511 months ago

      Correct, but only in the case of you import package.path.ClassName. That’s a fair bit different than import foo, which is just the top level “namespace” in Python.

      If you were to (for instance) do import package.path.*; it still is only going to actually import symbols you reference later in your code. So the point is you still have to reference TheSketchyClass to get it to take effect, whereas in Python it will happily do it at import, regardless of whether you use any symbols available via the import.

      The easy way to test this is to add your own static initialization block in a class named ImportExample in package import.test; with a System.out.println("hallo"); or something, then do import import.test.*;. As you can see, provided you don’t actually reference ImportExample anywhere in your own code, the static initialization block doesn’t actually get executed (though, if you did reference ImportExample, it would)

      Then again, while I was super deep into java until about 2015, I have no idea what the last 7 years of classloading have wrought upon my once-domain :)