Results 1 to 5 of 5
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    265
    Rep Power
    6

    Default How static is static?

    Alright, I understand what "static" is, but I have a question as to how static "static" is.

    Say there's an application that loads plugins. Now say I made two of these plugins, and they both reference another jar which I'll call "toolkit." If said toolkit were to maintain state information, would both plugin jars that reference it be referencing the same static object or...? I assume they would be, but I just wanted to confirm this. Thanks.

    I drew a picture to help illustrate my tangled words.

    (note: each of the 4 things below are individual jar files)
    Last edited by AndrewM16921; 07-07-2012 at 04:30 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: How static is static?

    It is variables and methods which are static rather than objects. Such static variables are associated with a class rather than any instance, so if a variable is static you can access it (assign things to it, assign its value to other variables, or pass its value to methods) via the class of which it is part.

    I'm a bit lost with the rest. "Application", "entry", "plugin", "toolkit", "referencing", "state", etc are all words which have whatever meaning they have at a higher (more abstract) level than Java. They might be just the thing for talking about Java code whereas "static" is used as part of that code. Perhaps you could post code? There seem to be 4 classes involved: at runtime a class like foo.bar.Baz is just a class, I don't think the .jar archive it was loaded from makes any difference, but your example could include .jar archives if that might matter.

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    265
    Rep Power
    6

    Default Re: How static is static?

    Well, four jars are involved. They each contain many classes. I will try to clarify.

    The "Application" JAR is the Runnable Jar that is run with something like "java -jar application.jar". I did not write this application, but I'm pretty sure it uses a class loader to load in any classes that extend its JavaPlugin class.
    Both "Plugins" are JAR files (not Runnable (i.e. no main)), which are loaded with the application's class loader. Generally speaking, they operate independently of each other, but I suppose they would be in the same Runtime? Both "Plugin" JARs reference (i.e. call static fields/methods in classes in the Toolkit JAR). The "Toolkit" is essentially has two purposes: utility methods, and maintaining state information between different Plugins. So, because (I think) they are in the same Java Runtime, the toolkit's static fields will refer to the same static object such that if one plugin modifies it it will be reflected in the other plugin's access of it and only one copy will exist? I hope that makes sense... lol.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: How static is static?

    If you have a class Toolkit with a static field int foo and if you say from anywhere

    Java Code:
    Toolkit.foo = 42;
    then if you say from anywhere - "application", "plugin" or Toolkit itself -

    Java Code:
    System.out.println(Toolkit.foo);
    then "42" will be printed. In that sense foo is static. "global" might be a better word as what is involved here is accessibility.

    -----

    If "42" is an important value throughout the code then it might fairly be described as shared state, shared via the common accessibility of the static Toolkit.foo.

    You are right to focus on the fact that there is one runtime. One runtime: one Toolkit.foo variable. A nice example of this (which has bitten me on the @ss) is to have a static variable in an applet class and then include two applet instances on a web page. I don't know if it is mandated but web browsers typically use a single Java runtime for both applets and, hence, they share the static variable (not what I intended).

    -----

    Notice in the forgoing that the Toolkit class was assumed to be loaded once by the runtime. That's important. If you load a class with the same name in different classloaders, each class will have its own static variables. (See, eg, discussion at JavaRanch.)

    I haven't had any experience of implementing a plugin mechanism but I imagine that care has to be taken not to load a plugin twice (in different classloader hierarchies) if the plugin's static stuff is meant to be global. But - as you described it - the global state is in the Toolkit class and that class is loaded just once like other "normal" classes.
    Last edited by pbrockway2; 07-07-2012 at 06:04 AM.

  5. #5
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    265
    Rep Power
    6

    Default Re: How static is static?

    Alright, that seems to have cleared it up for me a bit, thank you. That applet example was pretty close to what I was thinking of, though reversed. But yeah. Thanks. :)

Similar Threads

  1. Replies: 3
    Last Post: 05-31-2012, 08:56 PM
  2. Replies: 9
    Last Post: 06-10-2011, 01:40 AM
  3. Replies: 7
    Last Post: 09-24-2010, 06:09 AM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •