Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: "Help with answer to question from java 6 practice exam book!

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default "Help with answer to question from java 6 practice exam book!

    "Hi When faced with this question, I really gave up trying to work out what was going on and have given up in despair!
    Can you explain whats going on?
    Line 3 The 'singleton' identifier was very distracting, whats it doing? and the use of SafeDeposit just about everywhere has further confused me can anyone walk me through this code segment please"

    class SafeDeposit {
     
       private static SafeDeposit singleton;
     
       public static SafeDeposit getInstance(int code) {
     
        if(singleton == null)
          singleton = new SafeDeposit(code);
         return singleton;
    }
     
       private int code;
       private SafeDeposit(int c){code = c;}
       int getCode() {return code;}
    }
     
    public class BeSafe {
       //insert lots of code here
    }
    Last edited by Norm; November 14th, 2013 at 05:01 PM. Reason: Removed spaces in code tag


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: "Help with answer to question from java 6 practice exam book!

    singleton on line 3 is the name of a variable of type SafeDeposit

    SafeDeposit is the name of a class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default "Trying to understand question from java exam prep book"

    Quote Originally Posted by StanTheMan
    When faced with this question, I really gave up trying to work out what was going on can someone explain whats going on?
    line3 The 'singleton' identifier was very distracting, whats it doing? and the use of SafeDeposit just about everywhere further confused me. Could someone explain what is happening here please
    [code = java]
    class SafeDeposit {

    private static SafeDeposit singleton;

    public static SafeDeposit getInstance(int code) {

    if(singleton == null)
    singleton = new SafeDeposit(code);
    return singleton;
    }

    private int code;
    private SafeDeposit(int c){code = c;}
    int getCode() {return code;}
    }

    public class BeSafe {
    //insert lots of code here
    }[/code]

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: "Trying to understand question from java exam prep book"

    Why are you reposting this? Continue the discussion in your original post on this topic.

Similar Threads

  1. Can You Answer This basic Question "DDD
    By amiel in forum Java Theory & Questions
    Replies: 4
    Last Post: July 2nd, 2013, 09:08 AM
  2. Question about an exercise from D.S. Malik's Book "Java Programming"
    By Lahoree in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 15th, 2013, 01:47 AM
  3. "Pro Android 3" Book Example Code Question
    By mike duron in forum Android Development
    Replies: 1
    Last Post: April 8th, 2012, 12:43 PM
  4. Good examples for exam practice?
    By Twoacross in forum Java Theory & Questions
    Replies: 2
    Last Post: December 28th, 2011, 01:48 AM
  5. Need help with this practice java question
    By ZenithX in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 24th, 2011, 04:26 PM