473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

I lost the point: purpose of putting objects to null?

I lost the point:
What is purpose of putting objects to null:

If I make a class, JFrame for example, with button which start connection
dialog, which is class with connection and statement Object.
And every time I click the button first I put :
dialog = null
dialog = new dialog()

What is purpose to put dialog = null when all its objects are stay non-null?

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.725 / Virus Database: 480 - Release Date: 19.7.2004
Jul 17 '05 #1
4 4687
Dado wrote:
I lost the point:
What is purpose of putting objects to null:

If I make a class, JFrame for example, with button which start connection
dialog, which is class with connection and statement Object.
And every time I click the button first I put :
dialog = null
dialog = new dialog()

What is purpose to put dialog = null when all its objects are stay non-null?


There is no use in doing that.

Sometimes it's usefull to set a variable to null, to the memory it
occupies. But that's what you do after you have used the object, not before.

Edwin Martin.
--
http://www.bitstorm.org/
Jul 17 '05 #2
I personally use this to test if an object was ever set or not. If you
just check for (x == null) without actually SETTING x to null, then the
compiler would give you a "x may not be initialized" error.

Example:

java.sql.Connection c;

try {
c = java.sql.DriverManager.connect(...);
java.sql.Statement s = c.createStatement();
...
}
catch (Throwable thrown) {
// some code here
}
finally {
if (c == null) then c.close();
}

This example would not compile, because "c" is never initialized.
However, if we change the first line to:

java.sql.Connection c = null;

Then the finally block would work.
Dado wrote:
I lost the point:
What is purpose of putting objects to null:

If I make a class, JFrame for example, with button which start connection
dialog, which is class with connection and statement Object.
And every time I click the button first I put :
dialog = null
dialog = new dialog()

What is purpose to put dialog = null when all its objects are stay non-null?

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.725 / Virus Database: 480 - Release Date: 19.7.2004

Jul 17 '05 #3
You don't technically gain anything by doing this. However, I'm a believer
in declaring variables whenever possible at the top of a class or method.
Instead of having variable declarations scattered all over. So I tend to
have lots of lines like

MyObject _o = null;

at the top of a class or method. That way, when I see a variable and I
don't know what type it is, I can look at the top of the class or method and
quickly find out. You can just declare the variable without setting it to
null, but then your class might not compile unless every single method that
uses the variable makes sure it's been initialized.

Some coding philosophies consider it very bad form to declare and
instantiate a variable at the same time, in the body of a method, like this:

public void myMethod()
{

//Some code here
String myString = "ABC";

}

But this is more of a philosophy than a technology issue.
"Dado" <dz****@mail.inet.hr> wrote in message
news:ci**********@sunce.iskon.hr...
I lost the point:
What is purpose of putting objects to null:

If I make a class, JFrame for example, with button which start connection
dialog, which is class with connection and statement Object.
And every time I click the button first I put :
dialog = null
dialog = new dialog()

What is purpose to put dialog = null when all its objects are stay non-null?


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.725 / Virus Database: 480 - Release Date: 19.7.2004

Jul 17 '05 #4
Actually you do gain something in a server or long running application by
setting objects to null. These objects can be garbage collected.

Eric

In article <62*****************@newsread2.news.atl.earthlink. net>, "Dan
Nuttle" <d_******@hotmail.com> wrote:
You don't technically gain anything by doing this. However, I'm a believer
in declaring variables whenever possible at the top of a class or method.
Instead of having variable declarations scattered all over. So I tend to
have lots of lines like

MyObject _o = null;

at the top of a class or method. That way, when I see a variable and I
don't know what type it is, I can look at the top of the class or method and
quickly find out. You can just declare the variable without setting it to
null, but then your class might not compile unless every single method that
uses the variable makes sure it's been initialized.

Some coding philosophies consider it very bad form to declare and
instantiate a variable at the same time, in the body of a method, like this:

public void myMethod()
{

//Some code here
String myString = "ABC";

}

But this is more of a philosophy than a technology issue.
"Dado" <dz****@mail.inet.hr> wrote in message
news:ci**********@sunce.iskon.hr...
I lost the point:
What is purpose of putting objects to null:

If I make a class, JFrame for example, with button which start connection
dialog, which is class with connection and statement Object.
And every time I click the button first I put :
dialog = null
dialog = new dialog()

What is purpose to put dialog = null when all its objects are stay

non-null?



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.725 / Virus Database: 480 - Release Date: 19.7.2004


Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andrew | last post by:
Can someone help explain the following? Why is it that elements 5 - 9 in Bar::group do not seem to resolve to elements 0 - 4 of Bar::group when de-referenced? If I am following this correctly...
3
by: Goh, Yong Kwang | last post by:
I'm currently using execvp() for my lab program and I realise that the first argument (args) I pass to execvp is always not passed to the file ("/bin/ls") being executed. I'm using gcc compiler...
11
by: ricolee99 | last post by:
Hi everyone, I'm trying to invoke my .exe application from a remote server. Here is the code: ManagementClass processClass = new ManagementClass ("\\\\" +"RemoteServerName" +...
1
by: Brian Schloz | last post by:
Hello, I just thought I'd share my particular situation regarding session state periodically being "lost" in my asp.net app. I read with interest all of the posts regarding lost session state...
3
by: msteinwender | last post by:
Here is the scoop and any help is appreciated! In my Global.asax file I am creating an object and setting that object to an Application("obj") variable. The object gets created with no problems...
6
by: Lost Student | last post by:
Hello guys Any help that you can offer would be greatly appreciated. I am nearing the end of a C++ class and am so lost. The following is my assignment and what I have so far. Please help ...
27
by: jm | last post by:
I am having trouble understanding the purposes of an interface, even though the concept of interfaces is around me all the time (user interface, for example). I'm just not understanding software...
2
by: Nightcrawler | last post by:
I have the following tables that I have dragged into a .dbml file ( have only included the keys for simplicity). Users have a one to many relationship on Items keyed on UserId = UserId. Users...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.