Solved

set a shared value

Posted on 2002-07-25
3
200 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JUnit 4 @Before and @BeforeClass differences 3 60
Spring Framework HTTPSession management 1 36
fibonacci ten numbers 4 39
jdbc error in jsp application 20 46
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

803 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