Solved

set a shared value

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 241
ejb wildfly example 2 74
Is Applet the way to go for my drag and drop system? 8 35
Tagging and Merging on Branch 1 44
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

726 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