• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static and Non-static Methods

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does Java keep only one copy of a method in memory regardless of whether the method is static or non-static? Or are methods like variables--static methods: only one copy; non-static methods: one copy per object.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im gonna take a stab at answering this question from what I think.... I might well be wrong =D
With a static you would only have one copy. With a non static each method is associated with a specific object, and since each object is unique with respect to its state etc, then the methods associated with that object are also unique. Thus there is a copy of each method for each non-static object.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
While in the topic i was wondering what happens to a variable declared as final ? Does that also behave like instance variables or like static variables ? And what is the difference between Static and final variables ?
------------------
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A final variable can never be changed.
int i = 3;
final int varFinal = i ;
varFinal = 2 ; // will not compile
varFinal will forever after be 3. You have finalized it.
A final variable can be defined as a class variable or as a local variable or as an instance variable.
On the other hand, a static variable is a class variable.
int i = 3;
static int varStatic = i ;
varStatic = 2; // this is valid
There is only one reference (in this case "varStatic") but the value can be changed, and when it is, it is changed for every time "var" is referenced.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic