• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static keyword in Java  RSS feed

 
Shekar Chandu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

With respect to Static keyword, i have a question

In a normal scenario,we will have a class and we create an object for that class and invoke a mehtod on that object.
So to invoke a method we are ceating memory for an Object.

In case of static we say that Static belongs to class.
My doubt is class means just a blueprint for an object. So class has no memory.And when we say Static variable/method belong to class, what it exactly means.

Say for example



Here i can access Static variable a as A.a  .

But A doesnt have any memory right then how it can call "a" static variable.
 
Vaibhav Gargs
Ranch Hand
Posts: 116
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class, its metadata and static variables/methods are all loaded into memory before executing any code. So, it does occupy the memory.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not true that a class does not occupy memory. Classes are loaded into memory from *.class files. Also, see https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html
 
Shekar Chandu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:It's not true that a class does not occupy memory. Classes are loaded into memory from *.class files. Also, see https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html


So Junilu,you mean to say that each and everry class definiton that gets loaded into memory has a correspondng "Class" object.It is on this "Class Object" corresponding to class A we are calling static varables/methods?

Is there a way we can see whats inside "Class Object"?
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has an API that you can exercise. Where are headed with this? Why do you think you need to look "inside" it?

Also, you can't really look at references to class member variables and constants as "calls"; class methods, yes, you call them, but with class variables and constants, the dot notation is just the way you reference them in Java.
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes class's information is stored in method area. It's class not Class and static not Static, Java is case sensitive.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shekar Chandu wrote:
Is there a way we can see whats inside "Class Object"?

Under your JDK installation directory, hunt for an archive called src.zip. This archive contains the source code for all classes in the standard Java library.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!