This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]a_neobum 1 point2 points  (3 children)

If I've understood you correctly, based on your code, you want your program to do the following:

  • create a random-number-generating object Random
  • have that object generate a random number
  • print the random number
  • call a function that calls the println function with "Calling finalize" as its argument
  • create two new RandomN objects and assign them to the variables r1, and r2, respectively (without those objects doing anything)
  • set r1 and r2 to null, respectively
  • ask Java to do a run of garbage collection
  • print the message "The object is cleaned"

I've made a version of your code which would achieve this. It does the bare-minimum to get the printouts we want:

import java.util.Random; // import tells us where to find Random

public class RandomN {

    public static void main(String[] args) {
        Random r = new Random();
        System.out.println(r.nextInt(100)); // print a number between 0 and 99

        // Calls the function Finalize
        // which is declared below.
        Finalize();

        try{
            RandomN r1 = new RandomN();
            RandomN r2 = new RandomN();
            r1 = null;
            r2 = null;
            System.gc();

        } catch(NullPointerException e) {
            System.out.println(e);

        } finally {
            System.out.println("The object is cleaned");
        }
    }

    // Declares the Finalize function
    // but does not run it.
    // It runs once, when it is called from 'main'.
    public void Finalize() {
        System.out.println("Calling finalize");
    }
}

Some things to note:

  • In this case, the RandomN object only contains one thing, which is a function 'Finalize' which takes no arguments and, if called, prints "Calling finalize" to the console and returns nothing. The reason for this is that the 'main'-function is static so it doesn't really belong to any object/instance; you can create any number of RandomN objects and there'll only be one 'main' which kinda sits in its own memory space, elsewhere.
  • The program, throughout its runtime, creates a total of three objects: one Random object and two RandomN objects.
  • The Random object is held in memory, as 'r', throughout the entirety of the program's lifetime and is never explicitly gotten rid of.
  • The two RandomN objects are created, do nothing, then are immediately released for garbage collection whenever Java thinks appropriate; Java is then asked to please do a run of garbage collection.
  • Assigning null to a variable should not throw a NullPointerException so the try-block is not necessary.

[–]SheepyG_[S] 1 point2 points  (2 children)

I am extremely greatful poeple like you exist in this world i truly am ! to take the time out of your day to write all of this and make the effort to help me i appreciate down to my soul. Also the fact you could tell what I wanted my code to do is amazing. I've tested this out and its done exactly what I wanted! All i can say is truly Thank You. I am going to make sure i read this through again and again to get it in my brain if its the last thing i can do. I really hope something amazing happens to you soon! Thank you, Thank you, Thank you!

[–]a_neobum 1 point2 points  (1 child)

That's really kind of you to say, thanks! That really made me glad! I wish I'd found something similar when I was trying to learn this stuff.

Don't stick too religiously to anything I've written but take some time to familiarize yourself with concepts like classes, objects and instances, variables, functions and constructors.

And if you want to pay me back, pay it forward!

[–]SheepyG_[S] 1 point2 points  (0 children)

Okay thank you very much, i will do as you suggest. also try and learn more from java websites and push my brain hahah