Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 28th, 2006, 04:06 AM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default static field with class instance

Static variable is class variable and all the instance (object) of class share the single copy of static. It is none of use to call the static variable with class instance (object), though we can call.
I just wanted to know that how this functionality is achieved.
1.When class is loaded in memory, class variable i.e. static variable is created and when the instance (object) of that class is created, reference of that static variable is passed to the class instance (object)…
OR
2.At runtime, when static variable is used, compiler check that instance of which class invoke the static variable, and after identification compiler use the static variable from class memory…
So, what happens actually, either of two above or something else is happened when we call like “obj.staticfield”?
Reply With Quote
  #2 (permalink)  
Old July 1st, 2006, 07:22 PM
Authorized User
 
Join Date: Jun 2006
Location: boston, MA, USA.
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I guess the second option would be more efficinet , since it would be easy for the compiler to check wheter the class to which the object belongs to , has a "static" feild or method and if so then those can be directly called by the reference object .

Reply With Quote
  #3 (permalink)  
Old July 3rd, 2006, 02:41 AM
Friend of Wrox
 
Join Date: Jan 2006
Location: San Francisco, CA, USA.
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The implementation is actually a mix between your two approaches.  The compiler compiles references to static variables and methods into special bytecodes that specifically denote that the reference or call is static.  Then when the JVM executes the bytecodes, it knows to look up the static variables or methods in the class definition, rather than in an object instantiation.

More detail can be found here: http://mrl.nyu.edu/~meyer/jvmref/ref--19.html (Definition of the bytecode for retrieving a static class member.)

After reading up on this subject, I'm still confused how different classloaders fit into the situation.  Does a class loaded by two different classloaders have two instances of static member variables?  How does the getstatic bytecode know which classloaders to use -- does it always use the system classloader?  If anyone else has information on this, I'd love to hear it.

Jon Emerson
http://www.jonemerson.net/
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using logging class as a static class punitw General .NET 0 August 14th, 2008 04:03 AM
What instance? - Data-binding to my class stevefal Pro Visual Basic 2005 0 April 15th, 2008 07:02 PM
Instance of same class in main method ajit Java Basics 1 July 1st, 2006 07:26 PM
static Class in ASP chafor ASP.NET 1.0 and 1.1 Basics 4 January 13th, 2006 10:58 AM
who destroy static variables(no instance of class) MikoMax J2EE 1 March 31st, 2004 08:01 AM



All times are GMT -4. The time now is 12:35 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.