• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static reference

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi..
i'd like to ask about static object reference.
i have a class like this:


and the other one as a main class:

when i executed the TestMain class, the output is Size = 1 and when i excuted in other console, the output is still Size = 1.

Can you tell me how to make ouput Size = 2 while i execute in other console and always increase the size value if i execute in other window?

thanks in advance
 
Ricky Martaputra
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, there is a little mistake in my first code, the first class name should be TStaticImpl, not Test.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like you are executing the program and then -when it quits- start it again in the same (or another) terminal window. That means you're creating different JVMs. Variables (static or not) are only kept alive within a JVM, meaning you need to keep the JVM running, and then -when you repeatedly add values- will the number of objects increase.
If you want to persist objects between JVM invocations, have a look at serialization. If you want to keep the JVM up and running, some kind of application server would be required.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static variables are shared among all instances of a class within a single Java virtual machine (JVM). If you run the same program twice in two different console windows, you're running two separate, unrelated JVMs, and each has its own copy of that static variable. There's no way to make multiple JVMs share the same variable. What you would have to do is make multiple JVMs communicate with one JVM that "owns" the variable. This is called "distributed computing." There are many different ways to do this in Java -- RMI, CORBA, HTTP, raw sockets. You can learn about all of them in the Java Tutorial.
 
Ricky Martaputra
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hm..
i see...

thanks for your help...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic