java - Will other instances of a class be able to access a static variable in another? -


i have class know loaded urlclassloader each instance of it, if have static variable in one, other instances able access it?

for example, class myclass loaded classloader , classloader b, , want know if myclass loaded have same static fields myclass loaded b.

so basically, following statement true:

a.loadclass("myclass").getfield("myfield").get(null).equals(b.loadclass("myclass").getfield("myfield").get(null)); 

unfortunately @dinesh/@daj's answer incorrect. (@romain's maybe too, wording hard parse.)

suppose have class a.b.c , arrange same class gets loaded 2 different classloaders. according specifications, end 2 distinct class objects qualified name a.b.c, , type system perspective 2 distinct types. each of types have different set of static variables.

the primary reference jls 4.3.4 - paragraphs 2 , 3.

you can infer each reference type distinct (at runtime) have distinct set of statics jls 4.12.3, jls 8.3.1.1, jls 12.4, , other parts of language spec.


so basically, following statement true:

a.loadclass("myclass").getfield("myfield").get(null)     .equals(b.loadclass("myclass").getfield("myfield").get(null)); 

in general won't.

it true in some cases, depending on how myclass initializes myfield. instance, if field initialized literal string, will.

(the trick observing arrange myclass actually loaded 2 classloaders a , b, , not common ancestor classloader.)


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -