+ 3

What is the difference between these two?

Class c; Class c=new Class() ;

30th Jan 2018, 11:01 PM
Milan Srdić
Milan Srdić - avatar
2 Answers
+ 3
First one has no value (null). Second one has a value in device memory. "new" keyword means a "new space" on device memory. Obs.: you can't use null values once you catch the NPE (NullPointerException).
31st Jan 2018, 1:26 AM
Lucas Sousa
Lucas Sousa - avatar
+ 2
They are similar. First isn't initialized and second is Initialized. Class c; c is not initialized and would be null. So trying to use any of the functions in the Class Object on c would not compile. If later in the project you do: c = new Class() This would save space in memory as to only initialize it later when you need it. This would only make sence to me if c was a global variable. Then you can use the functions inside the Class Object: c.Function(); Class c = new Class() c is initialized to be a Class Object. You could then use the functions inside the Class Object: c.Function();
31st Jan 2018, 1:18 AM
Brendon B
Brendon B - avatar