Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6164811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:03:28+00:00 2026-05-23T22:03:28+00:00

If non- static functions are copied into the heap for each object that has

  • 0

If non-static functions are copied into the heap for each object that has that method, then why aren’t all methods in Java static by default? Why waste all of that heap memory this way?

A diagramic explanation would be more helpful for me to understand this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:03:28+00:00Added an answer on May 23, 2026 at 10:03 pm

    Typically, Java methods are not implemented by copying the method onto the heap once per object. Instead, methods are typically implemented using something called a virtual function table (or “vtable”). The idea is that there is exactly one copy of each method, whether it’s a static or nonstatic method, and pointers to these methods are laid out into a table. Each object in the heap then stores a pointer to the vtable for its object type. This means that the size of any heap object does not depend on the number of methods the object has. In fact, an object with 100 methods would be the same size an object with 1 method (assuming they have the same fields). Each just stores a pointer to the vtable for its object type, of which there’s only one copy.

    This optimization was originally used in C++ to support fast virtual functions and has since been used in many other object-oriented languages. It allows objects to be small, yet support dynamic dispatch.

    In other words, methods don’t need to be static by default because they don’t contribute to the size of the objects in the heap. Creating an object doesn’t take longer for objects with more functions, or does it use more heap space.

    Here’s one possible diagram for the layout of some objects (apologies for ASCII art!). Suppose that we have two classes, A and B. Then in memory, objects of those types might look like this:

       A                      vtable for A
     +-------------+        +---------------+
     | vtable ptr  | --+->  | method one    |
     +-------------+   |    +---------------+
     |             |   |    | method two    |
     | fields of A |   |    +---------------+
     |             |   |    |     ...       |
     +-------------+   |    +----------------
                       |    | method N      |
       A               |    +---------------+
     +-------------+   |
     | vtable ptr  |---+
     +-------------+
     |             |
     | fields of A |
     |             |
     +-------------+
    
       B                  vtable for B
     +-------------+     +------------+
     | vtable ptr  | --> | method one |
     +-------------+     +------------+
     |             |     | method two |
     | fields of B |     +------------+
     |             |     |    ...     |
     +-------------+     +------------+
                         | method M   |
                         +------------+
    

    Notice how both objects of type A share the same vtable, and how objects of types A and B only use the same amount of space for their vtable pointer, even if they have different numbers of methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is the following; I have a class that uses non-static native methods
I have a use case where I need to call a (non-static) method in
What are the disadvantages of making a large Java non-static method synchronized? Large method
The C++ specification says the default destructor deletes all non-static members. Nevertheless, I can't
I got really confused with static and non-static functions and class properties when I
If you code in C and configure your compiler to insist that all functions
I have a code behind page that has several method; one of them is
I have an interface that has ~16 input field boxes. They are all declared
Why does C++ require that user-defined conversion operator can only be non-static member? Why
What's the best way to connect a GTK+ signal to a non-static member function?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.