Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

does every object have its own method

 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

Kindly clarify this
Say i have a class with 3 non static methods
Now when i create 10 objects of this class
i think
each object would have its own set (copies) of the methods
Correct me if i am wrong.
This was the question asked in 1 of the interviews & they told me
that each object will have only copies of non static members
(variables) & not of non static methods

Rgrds
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes manish, you are correct... non-static member are not shared betwen object, however static members are.
Check the following code
 
Xara Mithra
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just Ran this program. Output as follows
t1.x=100
t2.x=100
t1.y=10
t2.y=0
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by manish ahuja:
i think each object would have its own set (copies) of the methods
Why would you need multiple copies of the methods?
 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thomas
I dont need multiple copies of amethod
what i wanted to know was whether each object would be having its own copy of non static methods
In case of static methods, there would be only 1
Jus wanted to know what is the behaviour in JAVA
Rgrds
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will be no multiple copies of method whether the method is static or non-static.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by manish ahuja:
I dont need multiple copies of amethod
Then why do you think Java would create multiple copies of a method?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic