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

  • Home
  • SEARCH
  • 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 211503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:06:05+00:00 2026-05-11T18:06:05+00:00

maybe it’s dumb but is there a difference between new Something().method(); and Something tmpSomething

  • 0

maybe it’s dumb but is there a difference between

new Something().method();

and

Something tmpSomething = new Something();
tmpSomething.method(); 

as I was calling only one method on this particular object, I chose the first solution but I’m not sure that this is exactly the same behavior…

I just want to mention the constructor initializes a Writer and method writes in this file…

  • 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-11T18:06:05+00:00Added an answer on May 11, 2026 at 6:06 pm

    I did a quick test. This is the extremely trivial test code:

    public class TestLiveness
    {
        public static void test1()
        {
            System.out.println(new Square(4).square());
            count();
        }
    
        public static void test2()
        {
            Square t = new Square(4);
            System.out.println(t.square());
            count();
        }
    
        private static void count()
        {
            for(int i=0; i<1000000; i++)
                System.out.println(i);
        }
    
        static class Square
        {
            private int val;
    
            Square(int val)
            {
                this.val = val;
            }
    
            int square()
            {
                return val * val;
            }
        }
    }
    

    Javap shows that the two methods are compiled differently; chaining doesn’t touch the local variable table whereas the temporary variable does indeed stick around until the method returns. However, the VM/JIT compiler may still perform liveness analysis and allow the instance to be garbage collected before the method returns.

    public static void test1();
      Code:
       0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
       3:   new     #3; //class TestLiveness$Square
       6:   dup
       7:   iconst_4
       8:   invokespecial   #4; //Method TestLiveness$Square."<init>":(I)V
       11:  invokevirtual   #5; //Method TestLiveness$Square.square:()I
       14:  invokevirtual   #6; //Method java/io/PrintStream.println:(I)V
       17:  invokestatic    #7; //Method count:()V
       20:  return
    
    public static void test2();
      Code:
       0:   new     #3; //class TestLiveness$Square
       3:   dup
       4:   iconst_4
       5:   invokespecial   #4; //Method TestLiveness$Square."<init>":(I)V
       8:   astore_0
       9:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
       12:  aload_0
       13:  invokevirtual   #5; //Method TestLiveness$Square.square:()I
       16:  invokevirtual   #6; //Method java/io/PrintStream.println:(I)V
       19:  invokestatic    #7; //Method count:()V
       22:  return
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

Maybe it's just me, but I cannot understand the documentation regarding Junit test integration
Maybe it's just doesn't exist, as I cannot find it. But using python's logging
maybe it's dumb but is there a difference between new Something().method(); and Something tmpSomething
...just like packages do. I use Emacs (maybe, it can offer some kind of
This question is for a web application. And maybe it's a stupid question but

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.