By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,874 Members | 1,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,874 IT Pros & Developers. It's quick & easy.

How do I determine how many times an application has started?

P: n/a
I have a Java desktop GUI application that the user can run multiple
times. In order to keep one instance of the application distinct from
another, I'd like to put the instance number of the application in the
title bar.

For example, the user starts the application, and the title bar says
"Control Panel". The user starts another instance of the application,
and it knows that one instance is already running, so the title bar of
the second instance says "Control Panel - 2nd" or something like that.

Is there some way that static methods and variables can keep track of
how many times the class has been instantiated, so that I could put that
information in the title bar?

I know I could create a little data file to do that, but hopefully there
is something simpler.

I do not need to keep track of when any instances stop.

Thanks,

Mark
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Mark Fisher wrote:
I have a Java desktop GUI application that the user can run multiple
times. In order to keep one instance of the application distinct from
another, I'd like to put the instance number of the application in the
title bar.

For example, the user starts the application, and the title bar says
"Control Panel". The user starts another instance of the application,
and it knows that one instance is already running, so the title bar of
the second instance says "Control Panel - 2nd" or something like that.

Is there some way that static methods and variables can keep track of
how many times the class has been instantiated, so that I could put that
information in the title bar?

I know I could create a little data file to do that, but hopefully there
is something simpler.

I do not need to keep track of when any instances stop.


Mark, you guessed the solution already. Just use a static variable to keep
track of the number of instances you have created. You can think of a
static variable as belonging to a class, rather than being associated with
a single instance of the class. Just have a look a this simple example and
it will help you solve your problem.

class Myclass {
static int count = 0;
Myclass() {
count++}
}

The variable count is static, meaning there is only one count, no matter how
many instances of Myclass you may have created. When the class is loaded,
the static variable is initialised at 0. When you create an instance of
Myclass, the constructor is called and the static variable count is
incremented, allowing you to keep track of the number of instances that
have been created. You can reference this static variable from your
instance using the class name, i.e. something like:
....
myTitleBarVariable = MyClass.count;
.....

Hope this helps,
Brgds
--
Suse Linux Professional 8.1 on Athlon 1.1 Ghz 512 Mb
Anti Spam = remove the "dot" and the "at"
Registered Linux User #264690
Visit us : http://users.skynet.be/herman.timmermans
Jul 17 '05 #2

P: n/a


Herman Timmermans wrote:
Mark Fisher wrote:

I have a Java desktop GUI application that the user can run multiple
times. In order to keep one instance of the application distinct from
another, I'd like to put the instance number of the application in the
title bar.

For example, the user starts the application, and the title bar says
"Control Panel". The user starts another instance of the application,
and it knows that one instance is already running, so the title bar of
the second instance says "Control Panel - 2nd" or something like that.

Is there some way that static methods and variables can keep track of
how many times the class has been instantiated, so that I could put that
information in the title bar?

I know I could create a little data file to do that, but hopefully there
is something simpler.

I do not need to keep track of when any instances stop.

Mark, you guessed the solution already. Just use a static variable to keep
track of the number of instances you have created. You can think of a
static variable as belonging to a class, rather than being associated with
a single instance of the class. Just have a look a this simple example and
it will help you solve your problem.

class Myclass {
static int count = 0;
Myclass() {
count++}
}

The variable count is static, meaning there is only one count, no matter how
many instances of Myclass you may have created. When the class is loaded,
the static variable is initialised at 0. When you create an instance of
Myclass, the constructor is called and the static variable count is
incremented, allowing you to keep track of the number of instances that
have been created. You can reference this static variable from your
instance using the class name, i.e. something like:
...
myTitleBarVariable = MyClass.count;
....

Hope this helps,
Brgds


Thanks for your reply! However, things do not quite work as I had hoped.

Here's the actual relevant code (running Java 1.5.0):

++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++
Jul 17 '05 #3

P: n/a
You want inter-application API's!

Static fields are for inter-instance commumication.

--
Regards,
Casey
Jul 17 '05 #4

P: n/a
Mark Fisher wrote:


Herman Timmermans wrote:
Mark Fisher wrote:

I have a Java desktop GUI application that the user can run multiple
times. In order to keep one instance of the application distinct from
another, I'd like to put the instance number of the application in the
title bar.

For example, the user starts the application, and the title bar says
"Control Panel". The user starts another instance of the application,
and it knows that one instance is already running, so the title bar of
the second instance says "Control Panel - 2nd" or something like that.

Is there some way that static methods and variables can keep track of
how many times the class has been instantiated, so that I could put that
information in the title bar?

I know I could create a little data file to do that, but hopefully there
is something simpler.

I do not need to keep track of when any instances stop.

Mark, you guessed the solution already. Just use a static variable to
keep
track of the number of instances you have created. You can think of a
static variable as belonging to a class, rather than being associated
with a single instance of the class. Just have a look a this simple
example and it will help you solve your problem.

class Myclass {
static int count = 0;
Myclass() {
count++}
}

The variable count is static, meaning there is only one count, no matter
how
many instances of Myclass you may have created. When the class is
loaded,
the static variable is initialised at 0. When you create an instance
of Myclass, the constructor is called and the static variable count is
incremented, allowing you to keep track of the number of instances that
have been created. You can reference this static variable from your
instance using the class name, i.e. something like:
...
myTitleBarVariable = MyClass.count;
....

Hope this helps,
Brgds


Thanks for your reply! However, things do not quite work as I had hoped.

Here's the actual relevant code (running Java 1.5.0):

++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++
.
.
// Keep track of number of times started
private static int instances = 0;

// main() to start things ======================================
public static void main(String[] args)
{
new CentralLimit();
}

// constructor ================================================
public CentralLimit()
{

CentralLimit.instances++;
// also tried -> instances++;

System.out.println("CentralLimit.instances: " +
CentralLimit.instances);
.
.
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++

From one command line, I start it -> java CentralLimit
and it says "CentralLimit.instances: 1". From another command line, I
start a second instance (java CentralLimit), while the first GUI is
still on display, and the reply is again "CentralLimit.instances: 1".

Any ideas?

Regards,

Mark

Mark, I 'm sorry, but I think I was a little to fast in replying. I was
referring to intra-application instances of classes, what you are looking
for is some form of inter-application ...
If you change your example like this :
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++
***.
***.
***//*Keep*track*of*number*of*times*started
***private*static*int*instances*=*0;

***//*main()*to*start*things*========================== ============
***public*static*void*main(String[]*args)
***{
****CentralLimit cl1 =*new*CentralLimit();
CentralLimit cl2 = new CentralLimit();
***}

***//*constructor*===================================== ===========
***public*CentralLimit()
***{

*****CentralLimit.instances++;
*****//*also*tried*->*instances++;

*****System.out.println("CentralLimit.instances:*" *+*
CentralLimit.instances);
***.
***.
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++

you'll see that instances = 2;

When it comes to counting how many times your application has been started,
I would suggest using a file containing an instance count. This file needs
to be created with the first start of the application and the variable
initialized to 1.
Subsequent application instances should check for 1) the file and 2)
increment the instance count. Upon termination, the application 1) reads
the file and 2) decrements the instance count, when the instance count
reaches 0 the application should delete the file before termination.

Brgds,
Herman
------------------------------------------------------------------
Suse Linux Professional 8.1 on Athlon 1.1 Ghz 512 Mb
Anti Spam = remove the "dot" and the "at"
Registered Linux User #264690
Visit us : http://users.skynet.be/herman.timmermans
Jul 17 '05 #5

P: n/a
Herman Timmermans wrote:
Mark Fisher wrote:


Herman Timmermans wrote:
Mark Fisher wrote:

I have a Java desktop GUI application that the user can run multiple
times. In order to keep one instance of the application distinct from
another, I'd like to put the instance number of the application in the
title bar.

For example, the user starts the application, and the title bar says
"Control Panel". The user starts another instance of the application,
and it knows that one instance is already running, so the title bar of
the second instance says "Control Panel - 2nd" or something like that.

Is there some way that static methods and variables can keep track of
how many times the class has been instantiated, so that I could put that
information in the title bar?

I know I could create a little data file to do that, but hopefully there
is something simpler.

I do not need to keep track of when any instances stop.
Mark, you guessed the solution already. Just use a static variable to
keep
track of the number of instances you have created. You can think of a
static variable as belonging to a class, rather than being associated
with a single instance of the class. Just have a look a this simple
example and it will help you solve your problem.

class Myclass {
static int count = 0;
Myclass() {
count++}
}

The variable count is static, meaning there is only one count, no matter
how
many instances of Myclass you may have created. When the class is
loaded,
the static variable is initialised at 0. When you create an instance
of Myclass, the constructor is called and the static variable count is
incremented, allowing you to keep track of the number of instances that
have been created. You can reference this static variable from your
instance using the class name, i.e. something like:
...
myTitleBarVariable = MyClass.count;
....

Hope this helps,
Brgds


Thanks for your reply! However, things do not quite work as I had hoped.

Here's the actual relevant code (running Java 1.5.0):

++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++
.
.
// Keep track of number of times started
private static int instances = 0;

// main() to start things ======================================
public static void main(String[] args)
{
new CentralLimit();
}

// constructor ================================================
public CentralLimit()
{

CentralLimit.instances++;
// also tried -> instances++;

System.out.println("CentralLimit.instances: " +
CentralLimit.instances);
.
.
++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++

From one command line, I start it -> java CentralLimit
and it says "CentralLimit.instances: 1". From another command line, I
start a second instance (java CentralLimit), while the first GUI is
still on display, and the reply is again "CentralLimit.instances: 1".

Any ideas?

Regards,

Mark


Mark, I 'm sorry, but I think I was a little to fast in replying. I was
referring to intra-application instances of classes, what you are looking
for is some form of inter-application ...
If you change your example like this :
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++
.
.
// Keep track of number of times started
private static int instances = 0;

// main() to start things ======================================
public static void main(String[] args)
{
CentralLimit cl1 = new CentralLimit();
CentralLimit cl2 = new CentralLimit();
}

// constructor ================================================
public CentralLimit()
{

CentralLimit.instances++;
// also tried -> instances++;

System.out.println("CentralLimit.instances: " +
CentralLimit.instances);
.
.
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++

you'll see that instances = 2;

When it comes to counting how many times your application has been started,
I would suggest using a file containing an instance count. This file needs
to be created with the first start of the application and the variable
initialized to 1.
Subsequent application instances should check for 1) the file and 2)
increment the instance count. Upon termination, the application 1) reads
the file and 2) decrements the instance count, when the instance count
reaches 0 the application should delete the file before termination.

Brgds,
Herman
------------------------------------------------------------------
Suse Linux Professional 8.1 on Athlon 1.1 Ghz 512 Mb
Anti Spam = remove the "dot" and the "at"
Registered Linux User #264690
Visit us : http://users.skynet.be/herman.timmermans

xmmmm

this id the wrong way ....

do you have heard the Singleton Patern ?

use singleton patern and make a counter class ..
this class initialize at the first time tha you will bi start the
aplication ...

at the second time you run the aplication and this go to initialize
again the VM see tha have already loaded this class and dont Initialize
again ...

so you Have a counter tha lives in VM ....
;)

Giorgos Maravelias
Java Certified Programmer
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.