File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Instantiate  a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instantiate  a class " Watch "Instantiate  a class " New topic
Author

Instantiate a class

Thomas Enquist
Greenhorn

Joined: Aug 11, 2003
Posts: 5
Hi,
i have a problem with my code, i am trying to find out if a particular class has been instantiated or not if it is not i create an object else i have to use a method in that class
what is the way i should approach to implement the above said logic any help would be greatly appreciated
thank you in advance
Thomas
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi Thomas,
Welcome to JavaRanch!
I have to guess what you mean by "if a particular class has been instantiated or not;" I'm guessing you mean whether a particular variable has been initialized or not -- something like this?

Maybe that "else" doesn't belong there; often in code like this you want to call myMethod() whether a new object was just created or not, so maybe you want


[Jess in Action][AskingGoodQuestions]
Thomas Enquist
Greenhorn

Joined: Aug 11, 2003
Posts: 5
the problem in detail
i have a timer which pops a jdialog at a certain intervals , the issue is i donot want to create different jdialogs for each and every timer task instead i would want to use thesame jdialog for all the timer tasks,
in the timertask run method iwant to find out if there was jdialog class has been initialised if yes perform the update of teh gui components else create a new jdialog.
for some reason even if the timer task just created a jdialog the next timer task still creates a new jdialog which i donot want
the if condition always seems to be true i.e m ==null .......
it doesnt go to the else part at point of time
please advice and thanks in advance
Thomas
Joe Pluta
Ranch Hand

Joined: Jun 23, 2003
Posts: 1376
I'm a little confused as to what you mean by "timer task". Are you creating a new Thread (or Runnable) object for each timer? If so, and you're using an instance variable to point to the JDialog, then you are going to get a new instance variable for each timer.
If you want all your timer objects to share the same JDialog, store it in a static variable:
private static JDialog timeDialog;
If this isn't the issue, then I need a little more information about your timer "task" and how you get it running.
Joe
Thomas Enquist
Greenhorn

Joined: Aug 11, 2003
Posts: 5
hi joe
i using java.util.Timer i.e
Timer t = new Timer();
t.schedule(TimerTask tt, Date d);
TimerTask tt has to implement a run method
and it is in this run method i am checking if the jdialog is instatiated or not and the jdialog Displays the current time it popped and a certain other time which is determined at run time .jdialog is its own class TimerDialog and has it owns gui components (buttons,comboboxes) which have actions associated with them.. hope this helps you if you need anything more holla at me
Thomas
Joe Pluta
Ranch Hand

Joined: Jun 23, 2003
Posts: 1376
And where are you defining the JDialog variable that you are checking?
Joe
Thomas Enquist
Greenhorn

Joined: Aug 11, 2003
Posts: 5
its Different Class RemindDialog.java with its own constructor and methods.
i am declaring RemindDialog rd ;
in the TimerTask and then checking in the if condition as follows
if (rd== null){
rd = new RemindDialog();
}
else{
rd.update()
}
for some reason rd is always null even tough the timer task has instantiated the RemindDialog before .......
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Joe is trying very hard to get you to tell us whether "rd" is a local variable, a normal member variable, or a static member variable. If it is the last of these three options, then the variable will be shared across all instances of the class; but if it's a local or normal member, then every time the initialization code is called, it'll be looking at a new variable that has never been set before.
So, to reiterate, unless the declaration of rd looks like this

then your code checking for null will probably always find null.
Thomas Enquist
Greenhorn

Joined: Aug 11, 2003
Posts: 5
that explains a lot i think i must have found teh solution to my problem
thanks every one
shall get to make it work and get back to you on the result
Thomas
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instantiate a class
 
Similar Threads
JList:Displaying a set of URL's
NoClassDefFoundError when executing java
please find this error in jboss please
JTabbedPane setting tab names with (sets? serialized objs, from file read?)
Inner Classes