How do I use multiple classes to access the same object?

This should be staightforward. I have and object and I have multiple GUIs which operate on the same object. Do all the classes creating the GUIs need to be inner classes of the data class? I hope this question makes sense.
Thanks,
Mike

public final class SingletonClass {
private static SingletonClass instance;
private int lastIndex = 10;
public final static SingletonClass getInstance()
if (instance == null) instance = new SingletoClass();
return instance;
public int getLastIndex() {
return lastIndex;
}1) This won't work since one could create a new SingletonClass. You need to add a private constructor. Also, because the constructor is private the class doesn't have to be final.
2) Your design isn't thread-safe. You need to synchronize the access to the shared variable instance.
public class SingletonClass {
    private static SingletonClass instance;
    private static Object mutex = new Object( );
    private int lastIndex = 10;
    private SingletonClass() { }
    public static SingletonClass getInstance() {
        synchronized (mutex) {
            if (instance == null) {
                instance = new SingletonClass();
        return instance;
    public int getLastIndex() {
        return lastIndex;
}if you are going to create an instance of SingletonClass anyway then I suggest you do it when the class is loaded. This way you don't need synchronization.
public class SingletonClass {
    private static SingletonClass instance=new SingletonClass();
    private int lastIndex = 10;
    private SingletonClass() { }
    public static SingletonClass getInstance() {
        return instance;
    public int getLastIndex() {
        return lastIndex;
}If you don't really need an object, then you could just make getLastIndex() and lastIndex static and not use the singleton pattern at all.
public class SingletonClass {
    private static int lastIndex = 10;
    private SingletonClass() { }
    public static int getLastIndex() {
        return lastIndex;
}- Marcus

Similar Messages

  • How do I use multiple local workspaces for the same website in VS2013?

    For quite some time I've been using VS2010 with multiple workspaces mapped from the same TFS branch to test different variants of a website in parallel. The internal web server (Cassini) seems quite happy to run multiple versions and simply maps a new port
    number for the second and subsequent instances that start up.
    We are now looking to move to VS2013, so have to use IISExpress since the 'internal' web server is no longer provided.
    How do I configure things so that if I open a single workspace, IISExpress runs against that workspace using the default port number (specified in the solution), but if I open a second instance in a different workspace it correctly uses a new port number
    and working folder. Do I need to invoke IISExpress using the command line (presumably from each workspace) and specify the relevant defaults or is there some way to enter this in the local config file for IISExpress?
    My initial attempt at editing to config file resulted in the 2nd and subsequent instance using the same physicalfolder as the first instance, presumably because the speficied site name and port number are the same!

    Hello,
    Thank you for your post.
    I am afraid that the issue is out of support range of VS General Question forum which mainly discusses
    the usage of Visual Studio IDE such as WPF & SL designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System
    and Visual Studio Editor.
    Based on your description, I suggest that you can consult your issue on ASP.NET forum:
    http://forums.asp.net/
     or IIS forums: http://forums.iis.net/ for better solution and support.
    Best regards,
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • How do I enable multiple profiles to access the same itunes library?

    Family of 4; with 4 different user profiles/log ins.  One family iTunes library; how can I allow all profiles to access the one master iTunes library?

    annafromsalt lake city wrote:
    how can I allow all profiles to access the one master iTunes library?
    move the entire iTunes folder to <MacintoshHD>/users/shared. on each user account, launch iTunes while holding the option(⌥) key, click on choose library when prompted, and select the iTunes folder you moved to the share folder.
    be aware only one iTunes at a time can access the library !

  • Accessing the same object from multiple classes.

    For the life of me I can't workout how I can access things from classes that haven't created them.
    I don't want to have to use "new" multiple times in seperate classes as that would erase manipulated values.
    I want to be able to access and manipulate an array of objects from multiple classes without resetting the array object values. How do I create a new object then be able to use it from all classes rather than re-creating it?
    Also I need my GUI to recognize the changes my buttons are making to data and reload itself so they show up.
    All help is good help!
    regards,
    Luke Grainger

    As long as you have a headquarters it shouldn't be to painfull. You simply keep a refrence to your ShipDataBase and Arsenal and all your irrellevant stuff in Headquarters. So the start of your Headquarters class would look something like:
    public class Headquarters{
      public Arsenal arsenal;
      public ShipDatabase db;
    //constructor
      public Headquarters(){
        arsenal = new Arsenal(this, ....);
        db = new ShipDatabase(...);
    //The Arsenal class:
    public class Arsenal{
      public Headquarter hq;
      public Arsenal(Headquarter hq, ....){
        this.hq = hq;
        .Then in your ShipDatabase you should, as good programing goes, keep the arraylist as a private class variable, and then make simple public methods to access it. Methods like getShipList(), addToShipList(Ship ship)....
    So when you want to add a ship from arsenal you write:
    hq.db.addToShipList(ship);
    .Or you could of course use a more direct refrence:
    ShipDatabase db = hq.db;
    or even
    ShipList = hq.db.getShipList();
    but this requires that the shiplist in the DB is kept public....
    Hope it was comprehensive enough, and that I answered what you where wondering.
    Sjur
    PS: Initialise the array is what you do right here:
    //constructor
    shipList = new Ship[6];
    shipList[0] = new Ship("Sentry", 15, 10, "Scout");
    " " " "etc
    shipList[5] = new Ship("Sentry", 15, 10, "Scout");PPS: To make code snippets etc. more readable please read this article:
    http://forum.java.sun.com/faq.jsp#messageformat

  • The Windows SMB feature has file locking if multiple users are accessing the same file.  Does Snow Leopard Server File Sharing (AFP) provide similar features?

    The Windows SMB feature has file locking if multiple users are accessing the same file.  Does File Sharing (AFP) on Snow Leopard Server provide similar services?

    Were you ever able to solve this problem. I'm having similar issues since upgrading to snow leopard. Four macs connect to a Windows Server 2003 for shared files. Each user has full permissions & when we "get info" it shows read & write permissions. Two of the computers were running 10.4, two were running 10.5. Everything worked properly until upgrading to snow leopard. Some files let me copy, move, delete. Others either just hang up or we get a "no permission" error. Also getting a "pdf is in use" error, even when the file/folder doesn't contain a pdf. We had our IT rep check the server who said everything is in working order. They don't represent macs any longer but feel that it's a mac problem. I would have to agree since this problem only started after the upgrade, and the one machine that was not upgraded (still running 10.5.8) is not dealing with these problems.
    Lastly, I would install 10.5 back on all of the computers if I could, but the leopard disk that came with one of the computers wouldn't work with the 2 machines running 10.4 and I didn't see it available at the apple store. I'll buy it if it's still available, but why wouldn't the disks that I have work?
    Thanks for any help

  • Hi, we have two iphones with two accounts.  The apple tv automatically syncs with one account to play music from the iphone to apple tv.  How can I use my iphone to do the same?

    Hi, we have two iphones with two accounts.  The apple tv automatically syncs with one account to play music from the iphone to apple tv.  How can I use my iphone to do the same?

    Our experience is a little different, we have different iTunes accounts for different family members, each family member has their own iTunes library on their own mac, each mac uses my Apple ID for homesharing and we are able to play purchased content from each library on the Apple TV.

  • Can I use multiple sa-ns500 on the same network?

    Is it possible to use multiple sa-ns500 speakers on the same network?  they seem to want to have the same name on the network and therefore only 1 will connect at a time?  Can I rename one somehow?  Thanks!

    Hello Brian,
    Welcome to Sony Community.
    Please check for the Secure Link option in the Home Theater System or A/V Receiver. This will help isolate the connection to the different Network speakers.
    Yes, you can use multiple Network speakers on the same home network.
    You can change the Device name using the Setting screen on the browser.
    Follow the steps mentioned on the page # 34 and 35 of the User’s manual to start up the browser of the Network speakers and change the Device name.
    If my post answers your question, please mark it as an "Accepted Solution."

  • How can i use offline & online database in the same time?

    how can i use offline & online database in the same time?
    my database in another server when the connection true it connect
    but if not does not work
    i wanna make offline database if the server not connected it connect offline then the server is on it alter all data from the offline to the server.

    User, please tell us your Jdev version!
    There is no such functionality build into the framework. The offline data base is only for designing the db, not to have a backup db.
    If you can't connect to the db the application will not work.
    Timo

  • E4200 v2 - Uses multiple MAC addresses for the same device

    This unit seems pretty good at first but on looking at the Web interface I noticed that it was using multiple mac addresses for the same device and showing devices on the lan on the Wireless side and other odd things.
    After a week of running it the home server network started to get erratic, it would seem to all point to dns and dhcp issues in the router.
    Seems the V1 had the same issues that they did not fix on this.
    The Cisco connect software shows 5 devices the Web based utility shows 3??.
    Its pretty bad that they charge nearly $200 and cannot be bothered to finish the unit or address the issues that were in V1.

    I am afraid I didn't get the clear picture of the issue mentioned in the post. Could you please elaborate a little bit? If you connect a device/computer wired and then later you connect it wirelessly. It is obvious that you would get to see two different MAC addresses for the same device, one for the LAN adaptor and other one for WLAN adaptor. If that is what you are referring to?
    Secondly, may I  know, what issues did you face with DNS and DHCP in the router? Is it not able to resolve the DNS or something else or DHCP is not sending IP address to the clients?

  • How to choose witch window will open external links when using multiple Firefox profiles at the same time?

    I am using 2 firefox profiles at the same, one for working and another for reading news. I start them with "firefox -no-remote -P profilename", so there are 2 firefox processes running.
    It works well, but when I click a link in an external app (like in Google Talk), the link is opened in the first firefox process I launched. I mean, if I ran the "news" firefox first, it will always open the external links.
    Is it possible to change this behavior? I'd like all external windows to be opened in the "news" firefox, never in the "working". Of course I'll achieve that if I open it first, but if I first run the "news", then the "working", then I close the news for any reason, now links will always open in the "working" until I close it.
    I running linux (opensuse 11.3).

    '''traysh''',
    Profiles '''''period''''' are "unsupported" by Mozilla. Profiles are "there" but the developers want no part of non-developers using them or fixing anything related to Profiles that has been suggested in a Bug report, or documenting much related to Profiles. Profiles as we know them now are going to disappear from Firefox in the near future, although no specific version number or date has been fixed for that to happen. It is up to us user's to share our experiences with Profiles with other user's. I'm a relative noob to Linux, and all my experience with running multiple Profiles and -no-remote is with Windows OS's.
    '''cor-el''',
    So are you saying that on Linux external links will only open in the first Profile that was opened if multiple Profiles are running?
    And that external links won't open a Profile that isn't already running - if any Profile is already running?
    ''I wish the differences between Windows and Linux as far as '''-no-remote''' and running multiple Profiles were better documented for "us" Linux noobs over at the MozillaZine KnowledgeBase - hint. Unless I am not looking hard enough in the MZ KB for that information....''

  • Multiple JSP's accessing the same thread

    I have multiple users calling multiple JSP's. I also now want to add in a connection pool, which is implemented as a Runnable (It's a thread). However, how do I set it up so that all of the JSP's get a reference to the same connection pool?
    I have done this in a Servlet, where I could have the connection pool constructed in the init() function, and then the Servlet kept a static reference to the pool. Then for each request, I pass the reference to the pool into the service() function of my supporting classes.
    How can I do this in JSP?

    There are few things.......
    I do not know which application server you are using , but almost all competitive application server provide the concept of connection pooling. You do not have to implement connection pooling yourself. And to get a handle to the connection from the pool is through JNDI namespace(as you do for EJB.)
    Now if you still want to do that yourself then, i can propose one way.
    1) Put a instance of the Java class which holds the connection pooling in the application scope.
    ie useBean= Yourclass scope="application"
    2) Now all the JSP's will be able to get to the same object.
    So Look for documentation for putting objects in different scope ie session, application etc.
    Srinivasan Ranganathan

  • How to avoid purchasing multiple things that are the same but different titles

    How can I avoid purchasing multiple things that are the same but have multiple names but is the same purchase for the same use?

    "Things"?  What things?  Apps for keeping track of when to change cat litter?  30 different versions of "twinkle, twinkle little star" played by everything from punk rockers to Gregorian chant?  Videos on the best way to make Christmas cake?

  • How can i create multiple apple ids from the same mac

    There seems to be a limit on how many apple ids can be created from the same cpu. Trying to create 100 apple ids for a client to use on ipads distributed to employees i found i could only create three or four before i got the message account cannot be created, contact itunes support. Had the same problem creating multiple itunes accounts through the App Store.

    I found this discussion because I need to create dozens of Apple IDs for the faculty and staff of the school for which I'm the IT Director.
    This is weak.
    Apple, PLEASE upgrade the Apple ID system so it represents the current needs and paradigm.
    What am I thinking, Apple (like Google) don't follow user discussion forums, except by accident when an employee sees something and is motivated enough to carry the water for users in need.

  • How do I view multiple inbox folders at the same time in Mail?

    I want to configure Mail to show me multiple inbox folders at the same time. I want to be able to split the screen and show in the top section unread messages, then messages I've marked with label A, then messsges I've marked with label B, then everything else in my inbox (like using the multiple inbox feature in Gmail). Is this possible?

    Just figured it out - I can select multiple messages as you suggested when I am not in full screen mode.
    So once I come out of full screen mode - then the multiple windows open.
    QED

  • CV04N-Searching across multiple classes that have the same characteristics

    In CV04N- is there a way to search across mutliple classes that have the same characteristics?
    For example I created 3 new class types each assinged to a new class, each class having the same characteristics.
    Say if I have a characterisitics called Contract Name.  I need to be able to search for a contract name across all 3 classes.
    The search results in CV04N defaults to class type 017 so it only shows characterisitcs relate to that type, Is there a way to get the characteristics for the new created class types to show?
    I know in CV04N you can only search one class type and class at a time.
    Does anyone know of a way to fulfill these requirements, without creating a customized report?

    Hi Jenny,
    The answer for this question is that:
    1. The class 017 document management in cv 04n is SAP standard class .It's totally different than the new class created by you.
    2. The new class in CL02 you are creating is for a perticular document type. When you assign a characteristic (contract name) to this new class ,it appears in additional data field as contract name.
    3. Now you can make a search with only Contract name filed and the report will be displayed with all document types having this characteristic (Contract name) is assigned. Thus you can find the classes(document types)thru characteristic(contract name).
    I hope this will resolve the query. And you will get clarified between two classes( i.e. 017 and new classes in DMS).You can say them as subclassed under 017 class.
    Regards,
    Ravindra

Maybe you are looking for

  • HELP!!!!!! - How to write into a file?

    How to write into a file? I want to make a script that will connect(not enter) to my website[for example, www.vasa.com] every time i connect to the internet. Now i have 3 problems: 1. i dont know how to write into a file 2. i dont know how to make it

  • I can't copy and paste.

    I recently updated my ipod generation 3 to ios 5.1 or something, and besides being kind of laggy, I can't copy and paste into different apps anymore. For example, I can copy and paste inside notes, but I can't, for example, copy a url from my notes a

  • Send mail sap office with notification

    Hi experts, I send an email in sap office (Business Workplace sbwp). I want send an automatic notification with this email for warning a user How can i do? Thanks in advance.

  • Why does my new macbook air keep crashing i.c.w. Time Capsule!!!?

    I bought my new MBA 11" @ the apple store eaton centre in Toronto. Since my purchase i dec 2010 the MBA crashed often but i thought this was normal (once a week). Now i connected a brand new Time Capsule & Airport Express (bought @ apple.com) and now

  • [CS5][Save for Web][Anti-aliasing] Fine lines in exported PSD with adjustment layers

    Hello, You can reproduce the bug this way: - link a PSD with a adjustment (Levels, Brightness/Contrast) layer in your AI file - Save for Web, with AA for Art (not text) selected In the exported image there will be artefacts, white lines usually. Flat