Solved

set a shared value

Posted on 2002-07-25
3
179 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
count11 challenge 6 47
Strange loading of website behaviour 3 23
ArrayIndexOutOfBoundException 9 30
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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

771 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

14 Experts available now in Live!

Get 1:1 Help Now