0
Class A {
A();
}

First way:

A a = null;

Second way:

A a = new A();

What is the difference between these two object declarations? Is there any eventual Memory Leak problem?

Houssam Badri
  • 2,441
  • 3
  • 29
  • 60

2 Answers2

3

The first method (A a = null;) doesn't create an instance of the A class so the variable's value is null.

The second method (A a = new A();) creates an instance of the A class at the same time as declaring the variable.

Memory leaks are not caused just by instantiating variables as you need to instantiate variables anyway to use them. This is a more complicated topic.

Szymon
  • 42,577
  • 16
  • 96
  • 114
3

What is the difference between these two object declarations?

They are not the same as you are trying to compare the both.

First case is you are not creating any Object of A, just initializing it to null.

Later you are creating an Object for A with new. Coming to the memory aspect there no such leak here.

Garbage collector do the stuff when you are no references to your Object.

//code
A a = null;
//codes 
a.something() // NullpointerException

case 2

    //code
    A a = null;
    //codes 
   a = new A();
    a.something() // No NullpointerException
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Can i use the first a without creating it? – Houssam Badri Dec 29 '13 at 12:03
  • No. You initialized it to null, so you run into NullPointerException if you without creating a new Object. – Suresh Atta Dec 29 '13 at 12:05
  • no you can't, because you will use 'nothing'. Your 'a' is a reference to your concrete object, you store link to that object in 'a' reference. SO if your 'a' is linking to 'null' so how can you use a 'null' value? Think – solvator Dec 29 '13 at 12:05
  • But carbage collector do it's job or not do it. So in some cases it's needed to force garbage collector to do it job with gc(). Also, Sometimes it takes too long, because collector goes with 2 circles : it marks unused objects, and leaves it for a while until it comes to delete it with 2nd circle. – solvator Dec 29 '13 at 12:09
  • Nice thansk, and the new() operations, does it initializes the object to null automatically? – Houssam Badri Dec 29 '13 at 12:19
  • Sorry, I did'nt get your question. What you mean by `does it initializes the object to null automatically?` – Suresh Atta Dec 29 '13 at 12:25
  • I mean when i make `A a = new A()`, does it have `null` value in memory or empty (because when we make `int i`, the compiler initialize it to null in memory) is that right? – Houssam Badri Dec 29 '13 at 12:28
  • No, When you do A a = new A() then memory allocates to the Object on heap. There is a clear difference between primitives and references. For primitives there is default value to each. So memory allocates on declarations it self. Where as for references memory creates when you initialized when you do `new`, AFAIK. – Suresh Atta Dec 29 '13 at 12:31
  • Thak you very much for all what u gave to me, on last question: What about String? is it Premetive or Reference, i mean can i create a String like: String s; too short?? – Houssam Badri Dec 29 '13 at 13:22
  • @HoussemBdr Yes. Glad to help you. String is very special in java. Please read :http://stackoverflow.com/questions/17489250/how-can-a-string-be-initialized-using/17489410#17489410 – Suresh Atta Dec 29 '13 at 13:25
  • @HoussemBdr Just informing. Avoid comments like +1 and perfect ..etc. If you really imress with answer, Please click upvote :) – Suresh Atta Dec 29 '13 at 14:02
  • @sᴜʀᴇsʜᴀᴛᴛᴀ got it : ) – Houssam Badri Dec 30 '13 at 05:11