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 8190381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:36:38+00:00 2026-06-07T03:36:38+00:00

In java there often objects that serve only to wrap a function and don’t

  • 0

In java there often objects that serve only to wrap a function and don’t have any state of their own. Example:

class Foo {
   void foo () {
      System.out.println("foo");
   }

   static public void main(String[] arg) {
      Foo foo = new Foo();
      foo.foo();
   }
}

I’d like to know if the expression new Foo() is optimized to what in C++ would be an assignment of function pointer. It seems like an obvious thing to do, but when I think about it, the compiler would have to check that foo is not used for synchronization (and possibly something else?). Does the standard say anything about 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-06-07T03:36:40+00:00Added an answer on June 7, 2026 at 3:36 am

    The standard (the VM spec and the JLS that is) doesn’t say anything about this, it’s entirely up to the VM implementations to deal with optimisations.

    All the standards specify is a set of invariants that need to be observed. If the VM guarantees that what you do will look like creating an object, then calling a method, then disposing of it, it can do what it wants.

    Although the exact manner of optimisation can vary, it is extremely rare that things on this low level are a bottleneck. But as optimisations are done runtime, you can be reasonably certain that they will only be performed if your code is invoked a lot; code that is infrequently used is interpreted rather than compiled into machine code. (But even this may change from VM to VM and may depend on specific command line options.) In your case what would probably happen is that after a lot of runs the VM inlines the method completely, getting rid of both the object creation and the method invocation.

    The one thing you can definitely do to “optimise” your code is to declare Foo (and foo()) final.

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

Sidebar

Related Questions

I have read a document that they say: In java there two types of
I have a question for java GC. I know that there is minor collection
I have a program that collects objects over time. Those objects are often, but
I know that more-dynamic-than-Java languages, like Python and Ruby, often allow you to place
When creating classes for immutable objects immutable meaning that state of instances can not
I listen to the podcast java posse, on this there is often discussion about
Java Method Class and Java 7's MethodHandle Class both refer to objects that are
I've noticed that Java, Python, Perl, and Haskell/Clojure have distinctively different treatment of static
I m looking at some Java code, and there is this code that I
In my project I often encounter Java heap space errors, i.e., there isn't enough

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.