• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the best way to declare a variable or objects?  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I have a code like below:


So, my question is what is the difference between these initializations
String str1; and String str2 = null; OR
Foo f1; and Foo f2 = null; because I'm going to create an Object of that references below anyway.

Does it make any difference if I explicitly assign null to the variable?
And tell me what is the best way to declare with null or without null?
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're only writing String str1;, you are not initializing the variable str1 at all; you are only declaring it. You must initialize it before you use it, and if you don't, you will get an error from the compiler. The same thing with Foo f1;.

If you write String str2 = null;, you're declaring variable str2 and you are also initializing it to null. See the difference with above - here, you are initializing the variable.

Note that null and "" (the empty string) are not the same thing. When a variable is null, it refers to no object at all. An empty string is a String object that contains zero characters.
 
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no difference in your declarations, default value for objects is null. Note that string is also an object not a primitive .
 
Mphatheleni Ernest Matidze
Greenhorn
Posts: 25
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper de Jong, you only get an error if the variable is a local variable. You can't use an uninitialized local variable. On the above class, those objects are not local so compiler won't complain. All those declaration are initialized automatically by the default constructor to their default value wich is null.
 
Mphatheleni Ernest Matidze
Greenhorn
Posts: 25
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parth A. Mehta, the best way to declare variables is to declare and initialize them with default values.
 
Parth A. Mehta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:If you're only writing String str1;, you are not initializing the variable str1 at all; you are only declaring it. You must initialize it before you use it, and if you don't, you will get an error from the compiler. The same thing with Foo f1;.

If you write String str2 = null;, you're declaring variable str2 and you are also initializing it to null. See the difference with above - here, you are initializing the variable.

Note that null and "" (the empty string) are not the same thing. When a variable is null, it refers to no object at all. An empty string is a String object that contains zero characters.


Ok I got it that I have to initialize it before using it But what about Foo class?

Is it a good practice to initialize it when declaring it with the null value like this:

Or leave it like:


I know if I have to use that object I have to do f1 = new Foo(); so adding null is good?
 
Mphatheleni Ernest Matidze
Greenhorn
Posts: 25
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best practice is to make it null. but it compiles fine even if you don't make it null, the only time it will give you problems is when you try to use it. It will throw NullPointerException.
 
Parth A. Mehta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mphatheleni Ernest Matidze wrote:The best practice is to make it null. but it compiles fine even if you don't make it null, the only time it will give you problems is when you try to use it. It will throw NullPointerException.


So, if I don't make it null then when I write compiler will initialize it with null that's why both are same, right?
I've asked this question because I've seen lot of code where the class begins with the lot of variable initialization to null value.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!