• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Subclass is not initialized...why?

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

This is possibly something which can be guessed easily...but i hope posting it here could bring more light on it..the situation is explained as follows;



class A { static int a = 1000; }

class B extends A {
static { System.out.print("B "); }
}

class Test {
public static void main(String[] args) {
System.out.println(B.a);
}
}


>> Now this prints 1000

>> But does B was initialized at all???

>> Why?

Have a good day
- Jay
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variables are not polymorphic. The compiler knows when it compiles class "Test" that the variable you've called B.a is really A.a -- class B is not needed to obtain its value. So a reference to A.a is compiled into Test.class, not B.a, and therefore B isn't loaded.
 
jay akhawri
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jess,

okey that make sense...but when classloader loads the Test.class in the VM , does it not also load the symbol table? and all the symbols are put in the symbol table at load time?.....does this mean the Linking process only take cares of such situations.....does that also mean Initialization only occures when Linking is done completly.....but what will be the case with Dynamic initialization?

Thanx
- Jay
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Jess?"

When a class is being loaded, yes, its "symbol table" (actually called the constant pool) is loaded, and entries in it that point to other classes cause those other classes to be loaded, too. In this case, class A will be loaded, because class Test will (as I tried to explain) contain a reference to class A, but not to class B, because the "real name" of B.a is actually A.a .
 
jay akhawri
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mr. Hill,

sorry for naming u wrong ..i had in mind that u wrote that book and i wrote that in mistake..i hope you wont mind.

Another of my doubts is that in the case discussed above envolves static linking...but if it were a dynamic linking ..wud compiler had to initialize all classes in VM, even if a subclass entity re-refers an already referred entity by superclass. what wud be flow of execution in that condition?

Have a nice day
- Jay
 
reply
    Bookmark Topic Watch Topic
  • New Topic