File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how does jvm behaves with static variables, methods and inner classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how does jvm behaves with static variables, methods and inner classes?" Watch "how does jvm behaves with static variables, methods and inner classes?" New topic
Author

how does jvm behaves with static variables, methods and inner classes?

Naresh Joshi
Greenhorn

Joined: Feb 13, 2013
Posts: 5

I have listen this "Java is not pure object oriented because of primitive types",
but what about the static fields ?
Does jvm provides an object for them or not ?
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 213

A class level Object is assigned to static fields.
you must have heard a class level lock in threading for static fields.


OCPJP
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Welcome to the Ranch If you search this forum for “pure object oriented”, you find many people have asked the same question before.
You would have to read the JVM specification to find whether the location of static members is specified. I think static fields live in the Class object, as do all methods. I presume static nested classes have Class objects of their own, referenced from the surrounding class’ Class object, bit I am not certain.
Naresh Joshi
Greenhorn

Joined: Feb 13, 2013
Posts: 5
Please explain it to me in detail .
And what is a class level object ?
Naresh Joshi
Greenhorn

Joined: Feb 13, 2013
Posts: 5
Campbell Ritchie wrote:Welcome to the Ranch If you search this forum for “pure object oriented”, you find many people have asked the same question before.
You would have to read the JVM specification to find whether the location of static members is specified. I think static fields live in the Class object, as do all methods. I presume static nested classes have Class objects of their own, referenced from the surrounding class’ Class object, bit I am not certain.


My question does not concern with why java is pure oop language or not,
In all the discussion everybody is telling java is not pure oop because of primitive types(because they are not objects).
What i am asking is static fields and methods are also not related to objects so why we do not give this reason ?
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 213

These static fields belong to an OBJECT. this object is a Class Level object.
Each class has it own "Class" object which can be obtained by ClassName.class
The static members belong to this object.
This is my understanding. Not pretty sure about it but still.

do have a look at this Java.lang .Class

thanks
Naresh Joshi
Greenhorn

Joined: Feb 13, 2013
Posts: 5
Ishan Pandya wrote:These static fields belong to an OBJECT. this object is a Class Level object.
Each class has it own "Class" object which can be obtained by ClassName.class
The static members belong to this object.
This is my understanding. Not pretty sure about it but still.

do have a look at this Java.lang .Class

thanks

thanks buddy, it is little bit satisfying.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Ishan Pandya wrote:These static fields belong to an OBJECT. this object is a Class Level object.
Each class has it own "Class" object which can be obtained by ClassName.class
The static members belong to this object.
This is my understanding. Not pretty sure about it but still.

do have a look at this Java.lang .Class

thanks
Why are you quoting Java 1.4? that has been superseded over 8 years ago.
The static fields do not really belong to the Class object. They belong to the class which corresponds to the Class object.
Yes, you can get the Class object by ClassName.class.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

Naresh Joshi wrote:In all the discussion everybody is telling java is not pure oop because of primitive types(because they are not objects).
What i am asking is static fields and methods are also not related to objects so why we do not give this reason ?


You could do that, if you liked. But the debate about whether Java is "pure" OOP or if not, why not, is in my opinion not very useful. It inevitably gets bogged down in a mess of competing definitions. However if this helps you to understand static members a little bit better, then it isn't a complete waste.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 213


Why are you quoting Java 1.4? that has been superseded over 8 years ago.

Ohh..Really sorry about that.. I just searched it directly from google and pasted the link.

The static fields do not really belong to the Class object. They belong to the class which corresponds to the Class object.

I got it.

Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 213

Naresh Joshi wrote:
thanks buddy, it is little bit satisfying.


Your welcome.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Naresh Joshi wrote: . . . thanks buddy, it is little bit satisfying.
But, as I said last night, little bit inaccurate too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how does jvm behaves with static variables, methods and inner classes?
 
Similar Threads
garbage collection and finalize method- inconsistent result
Super class constructors
init parameters for servlets
Servlet.service() for servlet action threw exception java.lang.OutOfMemoryError: PermGen space
Scala on mobile platform