• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic