When creating multiple instances of the same object, does Java replicate the method implementations?

user0000001

I've tried searching around for my answer but can't seem to find one.

I was curious if Java and or other modern languages optimize the replication of objects by doing some sort of virtual mapping for the methods. It would seem to be a waste if every time a new instance of a object is created, it would copy the methods associated with it rather then perhaps mapping these to one place in memory.

I can see some cases, such as polymorphism, where it might not work.

This might be more of a fundamentals question but I am very curious how the compiler handles this.

Thanks!

slim

Strictly speaking, it's none of your business:

The Java Virtual Machine does not mandate any particular internal structure for objects.

(JVM Spec)

So, if you were to write your own JVM, and for some reason you chose to put a copy of method code into every in-memory representation of an object, you would be free to do so.

However, there are various aspects of how the language is defined, that mean that it's not possible for two objects of the same class to have methods that differ -- even if they're non-static inner classes, dynamic classes, etc.

Therefore you're right that it would be wasteful of space to duplicate the method code for each instance, and no serious implementation of Java does so.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

docker-compose creating multiple instances for the same image

From Java

Declaring a method when creating an object

From Dev

Autofac with multiple implementations of the same interface

From Dev

EF saving multiple instances of same object

From Dev

Does Java call a method when an object is created at runtime?

From Dev

creating multiple instances of splFileObject

From Dev

c++ Object creating new instances of same type

From Dev

Why does Java call the overridden method even when object is upcasted?

From Dev

Unexpected behavior from shared_from_this when creating multiple shared_ptr "families" over same object

From Dev

Adding multiple instances of the same object to the stage

From Dev

Prevent NSMenu from creating multiple instances of same NSWindow when clicked

From Dev

does creating instance of the same class in the main mathod creates two instances?

From Dev

Creating multiple Instances of an Object in Structure C++

From Dev

Run multiple instances of same method asynchronously?

From Dev

Capturing multiple instances of the same group in java regex

From Dev

Java: Method hooking & Finding object instances

From Dev

Creating a New Custom Object vs Setting Multiple Values to Null of the Same Object in Java

From Dev

No such Method Exception, in generic method when creating new instances of a class

From Dev

Pass data to desired window when multiple instances of same window are opened Java

From Dev

IndexOutOfBoundsException when creating an object (Java)

From Dev

Adding multiple instances of the same object to the stage

From Dev

How Do I Run Multiple instances .java Files (running one of them as an Object or Method)

From Dev

does creating instance of the same class in the main mathod creates two instances?

From Dev

Creating multiple objects in java with for loop using the same object name to understand the finalize method

From Dev

Creating multiple instances of preparedstatement

From Dev

Multiple object sets are not supported. object sets 'ApplicationUsers' and 'Users' can both contain instances of ApplicationUser when creating roles

From Dev

Java: Method hooking & Finding object instances

From Dev

Creating multiple instances of the same id

From Dev

Getting a null pointer exception when trying to use an array to store multiple instances of the same object when creating a text-based 'database'

Related Related

  1. 1

    docker-compose creating multiple instances for the same image

  2. 2

    Declaring a method when creating an object

  3. 3

    Autofac with multiple implementations of the same interface

  4. 4

    EF saving multiple instances of same object

  5. 5

    Does Java call a method when an object is created at runtime?

  6. 6

    creating multiple instances of splFileObject

  7. 7

    c++ Object creating new instances of same type

  8. 8

    Why does Java call the overridden method even when object is upcasted?

  9. 9

    Unexpected behavior from shared_from_this when creating multiple shared_ptr "families" over same object

  10. 10

    Adding multiple instances of the same object to the stage

  11. 11

    Prevent NSMenu from creating multiple instances of same NSWindow when clicked

  12. 12

    does creating instance of the same class in the main mathod creates two instances?

  13. 13

    Creating multiple Instances of an Object in Structure C++

  14. 14

    Run multiple instances of same method asynchronously?

  15. 15

    Capturing multiple instances of the same group in java regex

  16. 16

    Java: Method hooking & Finding object instances

  17. 17

    Creating a New Custom Object vs Setting Multiple Values to Null of the Same Object in Java

  18. 18

    No such Method Exception, in generic method when creating new instances of a class

  19. 19

    Pass data to desired window when multiple instances of same window are opened Java

  20. 20

    IndexOutOfBoundsException when creating an object (Java)

  21. 21

    Adding multiple instances of the same object to the stage

  22. 22

    How Do I Run Multiple instances .java Files (running one of them as an Object or Method)

  23. 23

    does creating instance of the same class in the main mathod creates two instances?

  24. 24

    Creating multiple objects in java with for loop using the same object name to understand the finalize method

  25. 25

    Creating multiple instances of preparedstatement

  26. 26

    Multiple object sets are not supported. object sets 'ApplicationUsers' and 'Users' can both contain instances of ApplicationUser when creating roles

  27. 27

    Java: Method hooking & Finding object instances

  28. 28

    Creating multiple instances of the same id

  29. 29

    Getting a null pointer exception when trying to use an array to store multiple instances of the same object when creating a text-based 'database'

HotTag

Archive