• Post Reply Bookmark Topic Watch Topic
  • New Topic

how is the connection object not null  RSS feed

 
Nikki Tha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The output for the below code: "con is not null".
Could you please explain how ?

 
Paul Clapham
Sheriff
Posts: 22099
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably line 4 successfully assigns a non-null to the variable. Notice that there isn't any code which subsequently assigns null to the variable.
 
Henry Wong
author
Sheriff
Posts: 22646
112
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikki Tha wrote:The output for the below code: "con is not null".
Could you please explain how ?


How about ... the dbConnect() method returned a value that was set to the con variable?

Henry
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36170
439
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have this line of code which sets it to a non-null value


And nothing after that sets it to null. So you may have a closed connection at that point, but that doesn't make it reference null.
 
Nikki Tha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes dbConnect is assigning a valid connection.

But what is the difference between:
1.closing the connection
2. assigning it a null reference

i assumed that closing the connection means the variable which is holding it is having a null reference and ready for garbage collection.

Thanks


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65757
130
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikki Tha wrote:i assumed that closing the connection means the variable which is holding it is having a null reference and ready for garbage collection.

Not even possible. How can a method called from your code each back into your code and null out a variable? Think about that...
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36170
439
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikki Tha wrote:But what is the difference between:
1.closing the connection
2. assigning it a null reference

The former is changing the state of an object. The later is writing


Only one of these tells Java it is null and the object is allowed to be garbage collected
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!