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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:56:07+00:00 2026-06-17T10:56:07+00:00

If I have a method inside a class and I am creating an object

  • 0

If I have a method inside a class and I am creating an object inside that method
then would that object be destroyed and the memory allocated to it released
once the method is finished?

eg. –

public void drawFigure(){

    Paint paint = new Paint();
    paint.setSomeProperty();

    canvas.drawLine(startPoint, finishPoint, paint);

}

So after the method drawFigure is complete, paint object will be destroyed ?
which same as paint = null, but I do not need it to be set to null,
because it is a local object. Am I right?

  • 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-17T10:56:08+00:00Added an answer on June 17, 2026 at 10:56 am

    It is not guaranteed that object will be GCed as soon as method call is done, but object will become as eligible for GC and on next GC run it may be collected and memory will be free.

    EDIT:

    Yes you are correct. You don’t need to set it to null. Local variables will be created on stack and stack will be removed as soon as method is completed. So, paint will go away from memory and new Paint() object will be on heap without any references, which makes above object as eligible for GC.

    See this youtube video by Standford professor.

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

Sidebar

Related Questions

I have a class with event method that would often be called. Let's say
Setup: I have a COM DLL that calls a method inside a managed C#
Example: I have a method -myFooBarMethod:withFoo:bar:moreFoo: and inside the implementation of that method I
Using mootools I have a 'builder' class that manufactures form objects, dynamically creating divs
I have 2 classes. A and B. Inside ClassA I have a method which
I have a variable @products = Product.all defined inside new method in a controller.
Hi I have this simple method which gets triggered when i click inside a
I have a view inside a tabbarcontroller and navigationcontroller and my 'willAnimateRotationToInterfaceOrientation' method is
I have Activity with ListView inside it and in the onCreate method of the
Hello StackOverflow :) I have created some code inside my onStart(); method to ensure

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.