aspose file tools*
The moose likes Beginning Java and the fly likes Immutable Data structure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Immutable Data structure" Watch "Immutable Data structure" New topic
Author

Immutable Data structure

Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
HI! I have a question about Immutable data structure, specifically Immutable List. The following is part of the class that my teacher made:


Is line 04 declaring the object that the constructors below describe? Because usually the "new" always appears in the main class.
Then, I am not clear at what the append method on line 20 does. FYI: Since a list contains a value and a reference to the next list, head() is a method that gives the value/Object; tail() gives the reference.

Thanks for any clarification!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"new" is used whenever a new object needs to be created. Because the main method is an entry point for a Java program, that's a logical place to create a new object (or two, or three...) and start things off. But that's only a starting point. Once a program is running, there are lots of situations in which more objects are required.

The append method should look a little familiar, because it's recursive.
  • If "this" list is empty, then the method simply returns 'a' and no recursive calls are made.
  • If "this" list is NOT empty, then the method calls itself (on its own tail).

  • You might be having trouble with this line:

    To understand it, the "method chaining" could be broken apart (translated) as follows:

    In other words...
  • If "this" list is not empty, then it has a tail. So get that tail and call append(a) on it.
  • If that tail is not empty, then it has its own tail. So get that tail and call append(a) on it.
  • If that tail is not empty, then it has its own tail. So get that tail and call append(a) on it.
  • If that tail is not empty, then it has its own tail. So get that tail and call append(a) on it.
  • ...

  • Eventually, you will reach a tail that IS empty. That's the base condition of this recursion. When append(a) is called on that tail, it will simply return a, and execution will work its way back to the first caller of the method.

    Try working through simple cases. First, trace what happens if "this" list is empty. Next, trace what happens if this list has one element. Then trace what happens if this list has two elements...


    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    Cheryl Scodario
    Ranch Hand

    Joined: Nov 28, 2009
    Posts: 69
    marc weber wrote:"new" is used whenever a new object needs to be created. Because the main method is an entry point for a Java program, that's a logical place to create a new object (or two, or three...) and start things off. But that's only a starting point. Once a program is running, there are lots of situations in which more objects are required..


    Hi marc! What my friend was telling me was that because it is immutableList, so once created, you can't change it. Since it has two private constructors, it means you can't instantiate the ImmutableList object in another class/driver class. Only the class itself can. So on line 04, is it just instantiating the ImmutableList class object? And the two private constructors are then both referring to that? But I think only the 2nd constructor applies to that since it has no parameter as well. But then what's the point of having the first constructor if we don't use it? I hope you understand my question. thanksssss
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14111
        
      16

    Cheryl Scodario wrote:Hi marc! What my friend was telling me was that because it is immutableList, so once created, you can't change it. Since it has two private constructors, it means you can't instantiate the ImmutableList object in another class/driver class. Only the class itself can.

    That's right; the constructors are private, so that you can only create a new instance of ImmutableList from within the class ImmutableList itself.

    Cheryl Scodario wrote:So on line 04, is it just instantiating the ImmutableList class object?

    In line 4, an instance of ImmutableList is created using the constructor that takes no arguments (lines 11-14).

    Cheryl Scodario wrote:And the two private constructors are then both referring to that?

    No, the two constructors (lines 6-9 and lines 11-14) are not referring to the ImmutableList object that's created in line 4. Note that the second one (without arguments, lines 11-14) is used to initialize the ImmutableList object that's created in line 4. But the constructors are not themselves referring to the object in line 4.

    Cheryl Scodario wrote:But I think only the 2nd constructor applies to that since it has no parameter as well.

    That one is called to initialize the ImmutableList instance that's created in line 4.

    Cheryl Scodario wrote:But then what's the point of having the first constructor if we don't use it?

    The first constructor (lines 6-9) is not unused - it is used in line 17 in the push() method. Note that the push() method is inside class ImmutableList, so it can access the private constructors.

    Note that the push() method returns a new ImmutableList object - it doesn't change the ImmutableList object that you call that method on.

    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
    Scala Notes - My blog about Scala
    Cheryl Scodario
    Ranch Hand

    Joined: Nov 28, 2009
    Posts: 69
    Jesper de Jong wrote:
    Cheryl Scodario wrote:Hi marc! What my friend was telling me was that because it is immutableList, so once created, you can't change it. Since it has two private constructors, it means you can't instantiate the ImmutableList object in another class/driver class. Only the class itself can.

    That's right; the constructors are private, so that you can only create a new instance of ImmutableList from within the class ImmutableList itself.

    Cheryl Scodario wrote:So on line 04, is it just instantiating the ImmutableList class object?

    In line 4, an instance of ImmutableList is created using the constructor that takes no arguments (lines 11-14).

    Cheryl Scodario wrote:And the two private constructors are then both referring to that?

    No, the two constructors (lines 6-9 and lines 11-14) are not referring to the ImmutableList object that's created in line 4. Note that the second one (without arguments, lines 11-14) is used to initialize the ImmutableList object that's created in line 4. But the constructors are not themselves referring to the object in line 4.

    Cheryl Scodario wrote:But I think only the 2nd constructor applies to that since it has no parameter as well.

    That one is called to initialize the ImmutableList instance that's created in line 4.

    Cheryl Scodario wrote:But then what's the point of having the first constructor if we don't use it?

    The first constructor (lines 6-9) is not unused - it is used in line 17 in the push() method. Note that the push() method is inside class ImmutableList, so it can access the private constructors.

    Note that the push() method returns a new ImmutableList object - it doesn't change the ImmutableList object that you call that method on.


    Thanks Jesper. Your explanations made a lot of sense. So that means in the driver class, I can't instantiate the object using new at all because of private constructors? So the purpose of private constructor is to not let other class make any object of it? Line 04 is therefore both necessary and crucial? If without, we can't call any object? If we have public constructors, then we don't need line 04? would it be then redundant?
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14111
        
      16

    I don't know what you mean by the "driver class". Things that are private (for example, private member variables, private methods and private constructors) are only accessible from inside the class in which they are declared. So, you can't use the private parts of a class from any other class.

    If you wouldn't have line 4, then the class would be pretty useless, because there would be no way for the outside world to ever get an instance of the class, and you'd not be able to call the push() and append() methods anywhere, because you'd not have an instance to call them on.

    If you'd have at least one public constructor, then you could create a new instance of the class from the outside.
    Cheryl Scodario
    Ranch Hand

    Joined: Nov 28, 2009
    Posts: 69
    Jesper de Jong wrote:I don't know what you mean by the "driver class". Things that are private (for example, private member variables, private methods and private constructors) are only accessible from inside the class in which they are declared. So, you can't use the private parts of a class from any other class.

    If you wouldn't have line 4, then the class would be pretty useless, because there would be no way for the outside world to ever get an instance of the class, and you'd not be able to call the push() and append() methods anywhere, because you'd not have an instance to call them on.

    If you'd have at least one public constructor, then you could create a new instance of the class from the outside.


    Hi Jesper. By driver class, I meant main class. So how about if we change all the private to public now, then would line 04 still be necessary? Can we just remove it? Coz I think the whole point that line 04 exists is because of those private constructors. Can we just instantiate in the main class if now we have all the public constructors?
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14111
        
      16

    If you make the constructors public, then ofcourse you can instantiate the class from the outside. It wouldn't be necessary to have line 4.
    Cheryl Scodario
    Ranch Hand

    Joined: Nov 28, 2009
    Posts: 69
    Jesper de Jong wrote:If you make the constructors public, then ofcourse you can instantiate the class from the outside. It wouldn't be necessary to have line 4.


    Thanks, Jesper. I get it now.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Immutable Data structure