7 Replies Latest reply: May 29, 2014 1:52 PM by jschellSomeoneStoleMyAlias RSS

    Instantiating an object without an assignment

    SRAY01

      Hi,

      I am learning Java. I would like to know what is the significance of instantiating an object without an assignment.

      I have created a class TestClass1 with a single constructor that prints a test message.

      In TestClass2, if I write "new TestClass1()" rather than "TestClass1 x = new TestClass1()" it still works, and prints the test message.

      I would like to know if I do not assign an object at the time of instantiation, it cannot be referenced or reused later, then what is the significance of this type of construct and when it can be useful, and where is the object being held.

       

      public class TestClass1 {

         TestClass1() {

           System.out.println("This is a test message");

         }

      }

       

       

      public class TestClass2 {

        public static void main (String... args) {

          TestClass1 x = new TestClass1();  //This prints "This is a test message"

          new TestClass1();                 //This also prints "This is a test message"

       

          String y = new String("abc");  //this makes sense

       

          //The following does not throw an exception - what purpose could this conceivably serve

          new String("def");               

        }

      }

        • 1. Re: Instantiating an object without an assignment
          rp0428
          I am learning Java. I would like to know what is the significance of instantiating an object without an assignment.

          You have already answered your own question.

          if I do not assign an object at the time of instantiation, it cannot be referenced or reused later,

          Exactly - there is NO reference to it so 'it can NOT be referenced or reused later'.

          what is the significance of this type of construct and when it can be useful

          Again - you have answered your own question by posting an example of 'when it can be useful'.

              new TestClass1();                 //This also prints "This is a test message"

          You just produced the 'side effect' from the class 'TestClass1' without keeping a reference to it. If you need the side effect, one time only, that will do it.

          where is the object being held.

          The same place ALL objects are 'held'. It will be held until there is no longer a reference to it and then the garbage collector may 'collect' it and free the memoy on the hep.

           

          That sort of instantiation is not common, is not commonly needed and can make debugging more difficult since there is no reference to examine if you need to.

          • 2. Re: Instantiating an object without an assignment
            gimbal2

            What rp0428 said. But I can respond to one particular question in the code:

             

            //The following does not throw an exception - what purpose could this conceivably serve

            new String("def");  

             

            Answer: none, in the case of String. The String constructor doesn't do anything with a lasting effect.

            • 3. Re: Instantiating an object without an assignment
              SRAY01

              Apparently any method with a return value can be instantiated without an assignment.

              e.g. public static int methodInt() { return1; } can be instantiated as "methodInt();" besides "int i = methodInt();"

              So the feature is not restricted to constructors alone.

              • 4. Re: Instantiating an object without an assignment
                gimbal2

                > Apparently any method with a return value can be instantiated without an assignment.

                 

                That sentence makes entirely no sense. You don't instantiate methods. Do you know what the word 'instantiate' means? You are wickedly confusing yourself here, take a step back and re-examine the terminology before it gets worse.

                • 5. Re: Instantiating an object without an assignment
                  SRAY01

                  Sorry for using the wrong terminology - I mean a method is invoked rather than instantiated.

                  A Constructor can be treated as a special type of method to instantiate an object of the class - but it does not have any expressed return type - so I assumed it implicitly returns an object (of same type as the class the contructor belongs to).

                  But syntactically its usage is sort of similar to invoking a method (apart from the "new" keyword).

                  I was wondering how is it possible that a constructor or a method with a return value could be called in a code without an assignment. I assumed it should be possible only for void type of methods.

                  But apparently its a feature of Java. This feature is very different from any programming language I have worked with so far, e.g. PL/SQL where you cannot call a function with a return value without assigment.

                  • 6. Re: Instantiating an object without an assignment
                    skytrace

                    Try to read a fundamentals of Java by Cay Horstmann and Gary Cornell, they described your question in details.

                    • 7. Re: Instantiating an object without an assignment
                      jschellSomeoneStoleMyAlias

                      SRAY01 wrote:

                       

                      Sorry for using the wrong terminology - I mean a method is invoked rather than instantiated.

                      A Constructor can be treated as a special type of method to instantiate an object of the class - but it does not have any expressed return type - so I assumed it implicitly returns an object (of same type as the class the contructor belongs to).

                      But syntactically its usage is sort of similar to invoking a method (apart from the "new" keyword).

                      I was wondering how is it possible that a constructor or a method with a return value could be called in a code without an assignment. I assumed it should be possible only for void type of methods.

                      But apparently its a feature of Java. This feature is very different from any programming language I have worked with so far, e.g. PL/SQL where you cannot call a function with a return value without assigment.

                       

                      No the constructor doesn't 'return' anything.

                       

                      Construction is done by the new expression.  As PART of the execution of the new expression the constructor is called.  The new expression does other things besides just call the constructor.  For instance it allocates space and it is also responsible for returning the object.  And this is also true for languages like C# and C++ as well.