Solved

set a shared value

Posted on 2002-07-25
3
190 Views
Last Modified: 2010-03-31
Hi all

I need to set a value for useris and password to be shared by all classes.

the String user, password
is derived thru login.class

I have other classes that will need this 2 String later.

How can I put in a class so that I can retrieve it anytime I want??  to be input into another class?








0
Comment
Question by:TLTEO
3 Comments
 
LVL 4

Accepted Solution

by:
kylar earned 50 total points
ID: 7178047
Create a class like this:

public class IDHolder extends Object{
    private String m_userName = null;
    private String m_password = null;
    private IDHolder m_instance = null;
    private IDHolder(){

    }
    public static IDHolder getInstance(){
      if(null == m_instance)
          m_instance = new IDHolder();

       return m_instance;
    }

    public String getUserName(){ return m_userName;}
    public void setUserName(String newName){
        m_userName = newName;
    }
    public void setPassword(String newPass){
        m_password = newPass;

    }
    public String getPassword(){ return m_password;}

}


This is called a Singleton. There is only one instance of this class for your entire program, and it holds one user name, and one password, which is accessable from any other classes in your program. You can call it from anywhere by using the getInstance() call:

IDHolder.getInstance().setUserName("Foo");
IDHolder.getInstance().setPassword("Bar");

then retrieve it later..

String myUserName = IDHolder.getInstance().getUserName():
String myPassword = IDHolder.getInstance().getPassword();

Hope that helps!
Cheers,
Kylar
0
 
LVL 1

Expert Comment

by:klf
ID: 7178885
TLTEO

Kylar is correct.  If you have a multi-thread program you should make the getInstance method synchronized

 public synchronized static IDHolder getInstance(){
     if(null == m_instance)
         m_instance = new IDHolder();

      return m_instance;
   }
0
 
LVL 3

Expert Comment

by:randyd
ID: 7180931
the singleton is a good choice.

you could also make a id/password class instantiate one and place it as a System.setParameter() object.

this is then visible to any other class in the JVM.  

randy
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 73
java  and programming certification ? 4 86
bitbucket vs gitbucket 3 57
Apps blocked by Java 9 63
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now