Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 28th, 2006, 03: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)…
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”?
Old July 1st, 2006, 06:22 PM
Authorized User
Join Date: Jun 2006
Location: boston, MA, USA.
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts

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 .

Old July 3rd, 2006, 01:41 AM
Friend of Wrox
Join Date: Jan 2006
Location: San Francisco, CA, USA.
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using logging class as a static class punitw General .NET 0 August 14th, 2008 03:03 AM
What instance? - Data-binding to my class stevefal Pro Visual Basic 2005 0 April 15th, 2008 06:02 PM
Instance of same class in main method ajit Java Basics 1 July 1st, 2006 06: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

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.