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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:02:44+00:00 2026-06-04T08:02:44+00:00

For example, is this code valid?. class abc{ int x,y; abc(int x,int y){ this.x=x;

  • 0

For example, is this code valid?.

class abc{
    int x,y;
    abc(int x,int y){
        this.x=x;
        this.y=y;
        while(true)
            update();
    }

    public void update(){
        x--;
        y--;
        if(y==0)
            this=null;
    }
 }

If the above is not valid, then please explain why. I am in need of a class that after certain iterations ceases to exist. Please suggest alternatives to the above approach.

  • 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-04T08:02:45+00:00Added an answer on June 4, 2026 at 8:02 am

    No. The reason is that you do not make object null. When you say obj = null; You just put null to variable that previously hold reference to object. There are probably a lot of other references to the same object.

    I think that what you want to do is to kind of invalidate object and make it garbage collected but take this decision inside the class. If this is the problem I’d recommend you to take a look on weak references.

    Other possible solution is to implement kind of “smart reference” in java. You can create your class SmartReference that will hold the real reference to the object. The object should hold callback to this smart reference and call its method invalidate() that is something like your syntactically wrong expression this = null. You have to care not to refer to such objects directly but only via smart reference.

    The only question is “why do you want to do this?”. Really, this will cause the code to be more complicated and unstable. Imagine: the object decides to invalidate itself, so the reference that “smart reference” is holding becomes null. Now all holders of this smart reference will get NPE when trying to use the object! This is exactly the reason the such mechanism does not exist in java and that application programmer cannot mange the memory directly.

    Bottom line: remove all object references and let GC to do its hard job. Trust it. It knows to clean the garbage.

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

Sidebar

Related Questions

I have this code for example public class BaseClass {} public class DerivedClass1 :
Is this valid C# code ? public class Product { [CompilerGenerated] private string <Name>k__BackingField;
Is there anyway I can modify this code example #include <stdlib.h> #include <iostream> class
With regards this example from Code Complete: Comparison Compare(int value1, int value2) { if
Like this: public class remoteStatusCounts : RemoteStatus { public int statusCount; public remoteStatusCounts(RemoteStatus r)
I have this code structure: public abstract class ContentEntryBase { public string UniqueIdentifier; public
I'll attempt to shorten this code example: public interface IThing { //... Stuff }
The code: <?php class My_Test{ public $exists = 'yah'; public function test(){ return $this->exists;
For example: internal class C { public void M() { Console.WriteLine(foo); } } To
How i can get full right name of generic type? For example: This code

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.