5
class my_class {

    int a = 8;

    my_class() {
        System.out.println(a);
    }
}

public class NewClass {

    public static void main(String[] argue) {

        new my_class();

        new my_class();  

    }
}

I cannot understand the two statements in the main method ( new my_class(); ).

I have never seen this statement except in object definition. I know that the new keyword allocates memory for an object and assigns a reference address but what is happening in this case is totally ambiguous; allocate memory for what?

What does the new keyword do here? Whatever this is by using this statement I can explicitly call the constructor from the main method. I could not find such a statement anywhere in a textbook or the internet.

azurefrog
  • 10,785
  • 7
  • 42
  • 56
With A SpiRIT
  • 478
  • 3
  • 6
  • 17
  • 2
    this is called "bad code writing"... This uses the side effects of the constructor to display something on the console! – ParkerHalo Nov 23 '15 at 15:01
  • 2
    @ParkerHalo true and also see [this](http://stackoverflow.com/questions/7019754/what-does-the-new-keyword-actually-do-in-java-and-should-i-avoid-creating-new) – Neijwiert Nov 23 '15 at 15:02
  • 2
    Nothing wrong with this question. The equivalent in VB6 and VBA is not allowed. Logic alone does not address this IMO. – Bathsheba Nov 23 '15 at 15:04
  • You have never seen it "in object definition". You have seen it in a _variable assignment_ or in a _variable initialization_. `new Foo()` is a Java _expression_ that returns a value of type `Foo`. Just like any other Java expression, you can use it in a place where a value of type `Foo` is needed (e.g., assigning or initializing a `Foo` variable), or you can use it any place where a _statement_ is allowed. Statements in general do not produce values, so if you use an expression in a place where a statement is expected, the value will simply be ignored. – Solomon Slow Nov 23 '15 at 15:51
  • My professor plays with well concealed stuff that is never found in any text book.Don't know why these things matter to him.He always gives such questions in exams – With A SpiRIT Nov 23 '15 at 15:55
  • 1
    I might do the same if I were your professor. There is an awful lot of Bad Code out there in the world. Figuring out what Bad Code is supposed to do, and figuring out what it actually does (which may be different) are important job skills for any software developer. Of course, it also is important to recognize that it's Bad, and to know _why_ it's Bad, and to know how to make it better. I wouldn't leave those parts out of the lesson if I were your professor. – Solomon Slow Nov 23 '15 at 15:59

4 Answers4

6

new my_class() creates a new object of type my_class. It is not assigned; so it is discarded.

But before being discarded, the object is built anyway; the constructor is run and prints its object's a attribute value. 8.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
cadrian
  • 7,332
  • 2
  • 33
  • 42
2

Those two lines are creating objects of my_class and allocating memory in heap. But you can't refer those objects later since you are not storing the reference anywhere.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Rahman
  • 3,755
  • 3
  • 26
  • 43
2

new my_class(); not only calls the default constructor of my_class, but it also returns a reference to the newly created object.

You are, of course, free to discard that reference.

That is what is happening here. In not retaining a reference, the created object may well be eligible for garbage collection immediately.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

This code actualy creates 2 objects of my_class, but this objects are not bound with any reference so them will be deleted shortly by GC. This is similar to call as example.

int Check() { }

and it may be called

object.Check();

and it will works.

Mykola
  • 3,343
  • 6
  • 23
  • 39