-1

I am attempting to learn java in a training program, but am finding myself bottled up on some of the simpler things (like jargon).

One of my assignments requests the following: create an immutable class with two String variables. Then we are to instantiate this class.

public class Assignment {

    public final class Immutable {
        String one;
        String two;
    }

    public static void main(String[] args) {

        // Do I instantiate my class here?  
    }
}

For some reason the concept of "class instantiation" is escaping me :( Any help is appreciated on such a simple matter

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
kmancusi
  • 591
  • 3
  • 20

4 Answers4

2

If a class was a stencil, the image(s) you sprayed on the wall, using that stencil, were its instance(s). You can create many instances of one class, as long as you have enough memory (i.e. space on yor wall) available.

To create an instance, you need to use the keyword new to call its constructor, which is named after the class itself:

Immutable myFirstImmutable = new Immutable()

It is described pretty well in the Java Tutorial.

However, your class is not immutable, as it was possible to change the two fields any time:

myFirstImmutable.one = "some value"
myFirstImmutable.one = "oh no, another value"

Besides marking the class as final (to avoid it being subclassed), you also need to mark the fields (one and two) as final.

martin
  • 1,185
  • 17
  • 22
  • If the fields are declared as final, they must be populated at the time of instantiation. :) You cannot simply set "myFirstImmutable.one = "Data"; in this way. – Evan Bechtol Feb 06 '15 at 02:14
  • Of course :-) But since the OP didn't say anything about assigning values to those immutable string variables... ;-) I suppose, the assignment aims for a 2-argument-constructor, 2 final fields and 2 getter-methods to access the fields. – martin Feb 06 '15 at 02:30
1

Instantiation is a way of saying you are making an instance of the class.... not helping? Remember this is object oriented programming. A class is only the idea of an object. So you have to make an instance of the object ... or in other words, assign the "idea" of it to a variable. Stick with me....

So to make a class immutable you make it final. That's it. Immutable = final. It kinda makes sense if you think about it.

public final class Assignment{ 
   public Assignment(){
   }
}

That is the class and its constructor.

To use (instantiate) it...

Assignment asmt = new Assignment();

asmt is an instance of your class Assignment.

And now to add the variables...

public final class Assignment{
     private final String _one;
     public Assignment(){
     }

     public void setOne(String one){
          _one = one;
     }
     public String getOne(){
          return _one;
     }
}

And so finally to use the Assignment class:

Assignment asmt = new Assignment();
asmt.setOne("this var one");

Then to get the value...

System.out.println(asmt.getOne());
Menefee
  • 1,475
  • 1
  • 17
  • 26
  • 1
    I guess you forgot about the variables. – User27854 Feb 06 '15 at 02:10
  • By just making the class final, wont result in immutable class. The class that you created cannot be extended thats it. Immutable means: The contents of the objects is fixed and you wont be able to change it (So no setter methods or any methods that changes the state of the object). If you want to change the state of the object then it will or you will create a new object. I have posted my answer below. – User27854 Feb 06 '15 at 05:36
  • In your case the variables are not final, you have default constructor, setter methods. – User27854 Feb 06 '15 at 05:37
0

When you instantiate a class, you must actually declare a New object of the class type. This is done through the use of the new keyword. Please see the below example, using your code! Also, there is no need for the outer class in this example.

public final class Immutable {
    private String one;  //For Immutable  classes the variables must be declared as private so their values are not modified outside the class. 
    private String two;

   public static void main(String[] args) {

    // Do I instantiate my class here?  
    Immutable immutableObject = new Immutable (); // We just created an instance of the class
   }
}

Please remember, that while Java does provide built-in default constructors, it is good practice to create your own constructors. For example

public final class Immutable {
   private String one;
   private String two; //Must be private, it is also good to protect your variables

   // This is a default constructor
   Immutable () {
      one = "default value";
      two = "default value";
   }

   // Parameter-accepting constructor
   Immutable (String one, String two) {
      this.one = one;
      this.two = two;
   }


    public static void main(String[] args) {

    // Do I instantiate my class here?  
    Immutable immutableObject = new Immutable (); // Default
    Immutable immutableObject2 = new Immutable ("A string", "Another string"); // Now we defined the Strings in the instance!
 }
} // End class

By creating our own constructors, we can can be sure that our class is defined exactly the way that we intended.

User27854
  • 824
  • 1
  • 16
  • 40
Evan Bechtol
  • 2,855
  • 2
  • 18
  • 36
0

For a class to be immutable, the following points are must:

  1. Declare the class as final.
  2. Declare the members as private and final (private so that you are not able to access it from outside the class)

  3. Make access modifier of Default Constructor as private(there by fobidding the instantiation of the class)

  4. You will have only getter methods no Setter methods or(No Methods that modify state). You can even make it thread safe. Override the clone method so that no clone of that object is made etc..

Now the resulting class will be Immutable.

eg:

final class ImmutableInt {
    private final int value;

    public ImmutableInt(int i) {
     value = i;
    }

    public int getValue() {
     return value;
    }
}
User27854
  • 824
  • 1
  • 16
  • 40