• Post Reply Bookmark Topic Watch Topic
  • New Topic

When and where the methods of a class are loaded in jvm.  RSS feed

 
vikas byn
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When and where the methods of class are loaded in jvm.
If I have a class Test with method doStuff().

1) doStuff() is instance method.
If I make 1000 objects of this class then it will load and store this method for every objects?

2) doStuff() is class method.
If I make 1000 objects of this class then it will load and store this method only once?

3) to reduce memory usage, Is it good to make method static?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.
No.Yes. Sorry I misread your middle question at first.
No.

In reverse order, you make things static because they ought to be static. One per class, no differences from object to object. And for no other reason. you should not be worrying about memory, which is cheap.
Both instance and static methods live in the … it is all described far better than I could describe in the BCEL manual.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JVM never creates copies of the code of methods. The code of a method is fixed at runtime, it would be very wasteful to have multiple copies of the bytecode, and it's not necessary.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!