Results 1 to 2 of 2
  1. #1
    raffs03 is offline Member
    Join Date
    Sep 2012
    Rep Power

    Post Technical questions about Java garbage collection, class and main method

    Does java collects objects that have no reference pointing to it.


    Java Code:
    public static void main(String[] args)
       MyObject obj = new MyObject();
       obj = null;
    When the "obj" reference has changed its reference to null does that mean the created object(MyObject()) will be garbage collected or the program needs to complete its execution in the main method before the object is destroyed?

    Another question:

    Java Code:
    class AnotherClass
       void meth(){} 
    class MainClass
       public static void main(String[] args)
          AnotherClass ac = new AnotherClass();
    When I run this code does java automatically creates an object instance of MainClass or do I still need to use the new keyword?
    Does main method belongs to the MainClass?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Technical questions about Java garbage collection, class and main method

    First, GC is run periodically to clean up references that are out of scope (unreachable).
    This is done throughout the life of the program, not at the end.

    Second, main is an entry point and can be accessed without an instance of that class because it
    is declared static. So the class that has the main entry point must be instantiated within the program
    (possibly from within another class) using the new operator to create an instance.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Java Garbage Collection Interview Questions
    By jack12 in forum Reviews / Advertising
    Replies: 0
    Last Post: 11-30-2016, 01:39 PM
  2. General questions about Java Garbage Collection
    By tutiplain in forum New To Java
    Replies: 5
    Last Post: 11-19-2010, 04:57 PM
  3. Java Garbage Collection and destructors
    By riddhik84 in forum New To Java
    Replies: 1
    Last Post: 09-30-2009, 10:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts