Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

set a shared value

Posted on 2002-07-25
3
Medium Priority
?
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Accepted Solution

by:
kylar earned 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:
Suggested Courses

610 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