Forum Stats

  • 3,876,074 Users
  • 2,267,056 Discussions
  • 7,912,419 Comments

Discussions

Global Variable

Zabo
Zabo Member Posts: 188 Blue Ribbon
edited Sep 5, 2014 6:37PM in New To Java

Hello,

Is there a way to have global variable in Java associated to a process ?

I'm coming from PL/SQL world, I'm looking for something like package variables.

Thanks

«1

Answers

  • You can define variables like "static" clause. I don't know if you refer to this

    <span class="kwd" style="color: #00008b;">public</span><span class="pln"> </span><span class="kwd" style="color: #00008b;">static</span><span class="pln"> </span><span class="kwd" style="color: #00008b;">int xxx</span><span class="pln"> </span>

    HTH - Antonio NAVARRO

  • Zabo
    Zabo Member Posts: 188 Blue Ribbon

    In this case, every process has acces to the same value for the variable xxx.

    I would like that something like "public static" variables but every process would have its own value.

  • PhHein
    PhHein Member, Moderator Posts: 7,265 Silver Trophy

    Maybe you could use System Properties for that.

  • Zabo
    Zabo Member Posts: 188 Blue Ribbon

    It's not what I want to do.

    If I have to 2 users (so 2 processes) calling the same programm, I would like that User1 and User2 can store in a global variable XXX their name and then in any class I could have acces to their name by using XXX.

    Is there a way to do that ?

  • PhHein
    PhHein Member, Moderator Posts: 7,265 Silver Trophy

    What kind of process are you talking about? What are you trying to store in that global variable? System properties is one way to do it. There are others as well, like start parameters etc.

  • Unknown
    edited Aug 22, 2014 4:46PM
    If I have to 2 users (so 2 processes) calling the same programm, I would like that User1 and User2 can store in a global variable XXX their name and then in any class I could have acces to their name by using XXX.
    
    Is there a way to do that ?
     

    Sorry - you need to give a SPECIFIC example.

    1. What does user1 store in XXX?

    2. What does user2 want to retrieve from XXX?

    3. If user1 stores their name 'user1' in XXX then how would user1 retrieve 'user2' from the same XXX if that is what you mean?

    You've already been told system variables or preferences can store values that others can retrieve so it isn't clear why you can't use one of those.

    See the Preferences API

    Java Preferences API

    Applications require preference and configuration data to adapt to the needs of different users and environments. The java.util.prefs package provides a way for applications to store and retrieve user and system preference and configuration data. The data is stored persistently in an implementation-dependent backing store. There are two separate trees of preference nodes, one for user preferences and one for system preferences.
    
  • baftos
    baftos Member Posts: 3,431
    edited Aug 22, 2014 7:36PM

    Sorry, but I don't get the answers, other than Antonio's. The closest to a global variable is indeed a public static. And if process A sticks in there "Alice" and process B sticks in there "Bob", you have what you need. Process A will never see "Bob" and process B will never see "Alice". Nothing special to do, it just works like this. No matter how static and no matter how public the variable is, it is per process.

  • Sorry, but I don't get the answers, other than Antonio's.

    Possibly because we are all trying to help OP before they have really answered the question PhHein ask:

    What kind of process are you talking about?

    This is about all OP said:

    If I have to 2 users (so 2 processes) calling the same programm, I would like that User1 and User2 can store in a global variable XXX their name and then in any class I could have acces to their name by using XXX.

    If OP expects user1 to be able to access user2 data (OP did say 'global variable' - global to WHAT?) you can't just use ONE static variable and let any user change the value.

     The closest to a global variable is indeed a public static. And if process A sticks in there "Alice" and process B sticks in there "Bob", you have what you need. Process A will never see "Bob" and process B will never see "Alice". Nothing special to do, it just works like this. No matter how static and no matter how public the variable is, it is per process.

    A static might be 'global' to the process a particular user is using but it won't be global to the 'same program' that OP says all users are 'calling'.

    So, back to what PhHein was asking: what 'process' is OP talking about? A thread for each user running in the same VM? A thread running in the 'program' on some remote machine? What does 'global' mean? Global to ALL users? Global to the single process a single user is using? Why does a single process for a single user need to get back what the user name is? The user name can't be anything except that one user? What is the value of that? You could store that anywhere.

    Not enough info to understand the problem let alone know whether any particular solution is appropriate.

  • cesarHernandezGT
    cesarHernandezGT Member Posts: 42 Blue Ribbon

    Sounds like a design problem. What exactly are you trying to accomplish?

    What kind or process?

    Remote machines involved?

  • jwenting
    jwenting Member Posts: 4,864 Gold Badge

    hmm, if he wants it to work like package level variables in PL/SQL it'd need to be shared between VMs running on different machines accessing the database, as package level variables are shared between all clients accessing that PL/SQL package.

    That's of course way outside anything you can expect from Java to do for you... Obvious way would be to have a central server that does the processing for all those clients and if you do that, why not just stick with those PL/SQL packages as they do effectively just that...

    Alternative could be a table storing the "variable" that all clients can access, but that of course leaves you with transaction isolation problems.

    So yes, he has a major design flaw most likely, as does pretty much anything that requires globals.

This discussion has been closed.