Results 1 to 8 of 8
  1. #1
    Willsy is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Can a class make instances of itself?

    Hello all, this is my first post.

    I *think* the answer to this question is no, but...

    Can a class make instances of itself, in a kind of recursive way:

    Java Code:
    class FileProcessor
       public FileProcessor ( String filename ) {
         // constructor
       }
    
       private void processStuff () {
          if(foundFileToProcess) {
            FileProcessor fp = new FileProcessor(newFileToProcess);
          }
       }
    }
    Seems like to easiest way to process a file that may contain references to other files that need to be processed, and then resume processing the original file, but in practice I'm sure it's more complex than that. Still, if possible, i'd to begin with this kind of concept. Is it possible?

    Regards

    Willsy

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: Can a class make instances of itself?

    It is possible, but you need to be careful of infinite recursive calls.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Can a class make instances of itself?

    It's done all the time and is an integral part of the factory design pattern where instead of passing parameters to a constructor to specify every aspect of some class, you call a method whose name describes the basic behavior. It can be fine tuned with additional parameters to the creating method. In some cases the returned instance is one of several possible interface implementations. And finally, a class creating an instance of itself is also used to create singletons where the first call creates an instance of itself and subsequent calls return the instance. An good example of the Factory design pattern in the JDK is the BorderFactory class.

    Regards,
    Jim
    Last edited by jim829; 11-27-2013 at 03:24 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Willsy is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Re: Can a class make instances of itself?

    Interesting. What I'm not seeing at the moment is how one avoids memory leaks. For example, in the pseudo-code that I posted above, each new instance of the class gets the same name. Imagine:

    Thing a=new Thing();
    Thing a=new Thing();

    Okay, that's not recursive, but the first instance, is, as I see it, is effectively lost forever, as the second line of code creates a new instance of thing. And that's where I am currently puzzled with object-level recursion; the previous instance(s) being overwritten by the new one, such that you can never get them back.

    Regards

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: Can a class make instances of itself?

    you can never get them back.
    The garbage collector should reclaim the memory it uses when there are no connections to the object.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Can a class make instances of itself?

    In regard to memory leaks, there are none. If the first instance is no longer reachable by a hard reference then it will be garbage collected.

    With regard to a class making instances of itself, this is what I thought you were talking about.

    Java Code:
    public class ClassDemo {
       public static void main(String[] args) {
          MyClass[] myClasses = new MyClass[10];
          for (int j = 0; j < myClasses.length; j++) {
             myClasses[j] = MyClass.newInstance(j);
          }
          for (MyClass m : myClasses) {
             System.out.println("value = " + m.getValue());
          }
          
          // or
          
          MyClass b = MyClass.newInstance(20);
          MyClass c = b.newInstance2(30);
          System.out.println("b Value = " + b.getValue());
          System.out.println("c Value = " + c.getValue());
       }
    }
    
    
    class MyClass {
       private int a;
       // private constructors - can't instantiate
       // outside of class
       private MyClass() {}
       private MyClass(int a) {
          this.a = a;
       }
       public static MyClass newInstance(int b) {
          return new MyClass(b);
       }
      
       public MyClass newInstance2(int b) {
          return new MyClass(b);
       }
       public int getValue() {
          return a;
       }
    }
    You can also do the following but it is a recursive definition (sort of like in C where a structure contains a pointer to a structure of its own type).

    Java Code:
    class Node {
       Node leftNode;
       Node rightNode;
       Object payload;
    }
    Regards,
    Jim
    Last edited by jim829; 11-27-2013 at 12:04 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Can a class make instances of itself?

    Object.clone() also comes to mind. Objects creating instances of itself are an integral part of the platform.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Can a class make instances of itself?

    Yep! Totally forgot about that.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. How to get multiple instances of an object/class
    By xEthan in forum New To Java
    Replies: 3
    Last Post: 08-04-2013, 01:40 AM
  2. How to paint multiple instances of class?
    By Zigster in forum AWT / Swing
    Replies: 5
    Last Post: 05-29-2012, 04:32 PM
  3. Error initializing class instances
    By fr0s1yjack in forum New To Java
    Replies: 1
    Last Post: 06-22-2011, 03:58 PM
  4. Class Instances stored in an ArrayList
    By Ersk in forum New To Java
    Replies: 4
    Last Post: 12-12-2009, 04:13 PM
  5. Problem with class instances
    By sdwinder in forum New To Java
    Replies: 7
    Last Post: 10-21-2009, 01:25 AM

Tags for this Thread

Posting Permissions

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