Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instantiate a class

 
Thomas Enquist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Thomas Enquist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1376
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1376
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And where are you defining the JDialog variable that you are checking?
Joe
 
Thomas Enquist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic