aspose file tools*
The moose likes Java in General and the fly likes Static classes and Classloaders Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Static classes and Classloaders" Watch "Static classes and Classloaders" New topic
Author

Static classes and Classloaders

Justin Russo
Ranch Hand

Joined: Oct 21, 2007
Posts: 77
Can some one explain me the internals of static classes. Does it have one instance loaded in memory by the JVM.

Also what are classloaders? i know that classloader implies loading classes but then who loads the classloader class?


You Want it.. Get it.......the Right Way...<br /> <br />SCJP 5.0 SCWCD 5.0
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The only kind of "static class" is a nested class marked "static", and in fact these are extremely similar to normal top-level classes. A nested class that's not marked static is associated with one specific instance of the containing class; a static nested class is not. That's the only difference.

As far as classloaders go: a JVM almost always has more than one ClassLoader; at the very least, there's generally one that loads the "system classes" (the java.* classes and such that are "built in") and another ClassLoader than loads your classes.

The very first ClassLoader is assembled "by hand" by the JVM. Ultimately a class is just a data structure inside the running JVM program; the first couple of classes in a running JVM are put together just by initializing those data structures to "bootstrap" the class loading process.


[Jess in Action][AskingGoodQuestions]
Justin Russo
Ranch Hand

Joined: Oct 21, 2007
Posts: 77
Thanks..

I have one more doubt..

When we have static methods/variables in a classes we can access it without creating an instance of that classes.

this is enforced only by the compiler ?

the Jvm works regardless of whether a method is static od nonstatic.

All references are assembled at compile time?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Justin Russo:

this is enforced only by the compiler ?

the Jvm works regardless of whether a method is static od nonstatic.


Hmmm. You might need to give this some thought first.

A static member variable can be referenced without an instance because the value is stored outside of any instance; an instance variable is stored in an instance of the class, so without an instance to reference, there is no variable.

For methods, in a way, that's true; the code doesn't live in each instance, there's just one copy of a method's code tht covers the whole class. But the implicit first argument of an instqnce method is an instance of that class, and since many instance methods operate on instance data, without an instance to operate on, the method wouldn't be able to work. The only instance methods that could be called without an instance (in theory) are those that don't refer to the instance at all -- such methods could have a "static" declaration added to them, and still compile.
Justin Russo
Ranch Hand

Joined: Oct 21, 2007
Posts: 77
Thanks again...

sorry but one last thing!!

By loading a class means the class loader puts the byte code present in the .class file in memory ?

and

By instantiating a class means calling the constructor?

thnx
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

1) Yes, pretty much. Loading the bytecode, creating space to hold the static members, initializing them, etc (technically, loading the bytecode and initializing the static data are two separate things, and one can happen before the other -- but in practice we generally talk about them both together.)

2) Allocating a block of memory to hold the object's instance variables, and then calling the constructor, yes. Java's "new" operator does both of these things.
Justin Russo
Ranch Hand

Joined: Oct 21, 2007
Posts: 77
thanks!.. this has cleared a lot of doubts i had...
 
 
subject: Static classes and Classloaders