473,671 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4699
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.Connec tion c;

try {
c = java.sql.Driver Manager.connect (...);
java.sql.Statem ent s = c.createStateme nt();
...
}
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.Connec tion 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.in et.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.earth link.net>, "Dan
Nuttle" <d_******@hotma il.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.in et.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
1548
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 I would assume the following is happening. Elements 0 - 4 of Bar::group are created when Bar mainlist is declared in main.
3
3453
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 (3.3.1) running under Cygwin from Redhat. I've done a test program which looks like this: main(){ char* args;
11
5550
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" + "\\root\\CIMV2:Win32_Process");
1
12864
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 (virus scanner, modified assemblies/configs, etc.). The suggested solution of using a 'State Server' or 'SQL State Server' was not attractive to me given the drawbacks: performance, objects must be serializable, no more Session_End.
3
2035
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 and the application variable is set successfully to that object. Once the Global.asax page has finished the Application_Start routine the startUp page (deploy.aspx) is loaded. On that page my application variables are now null. Even application...
6
1673
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 Define a class called Point which contains the coordinates (x, y) of a point in a plane, where both x and y are two integers (positive or negative whole numbers), i.e. the Point class contains two private
27
3842
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 interfaces. Like anything else, it appears Interfaces are by design something that requires documentation. I know this may be obvious, but if I have a class A and I say, well, class A implements IMyInterface. The fact that it implements...
2
1738
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 have a one to many relationship on UserFriend keyed on UserId = UserId and UserId = FriendId CREATE TABLE .( NOT NULL,
0
2186
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 problems later). Apparently Point is a struct, a value type, and it does not behave like a classic structure (in my mind's eye, and see below). Traditionally I think of a classic structure as simply an object where every member is public. But with...
0
8483
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8824
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8673
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7444
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6236
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5703
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.