Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28
  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Strings are not printing as they should?

    all the counters get set back to zero and I don't understand why.
    Does the code set the current counters to zero
    or is a new instance of a class created with the counters set to zero?

    For either case, the debug printlns should show you what is happening.
    Fof the first case: print out a message every time the values of the counters are changed showing the values of the counters.
    For the second case: print out a message every time a new instance of the class is created with zero values.

    The print out will show you why and where the counters are being reset to zero.
    If you don't understand my response, don't ignore it, ask a question.

  2. #22
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: Strings are not printing as they should?

    Ive concluded that every time the program executes the class, the counters get set to zero again. Why do they continuously get reset? Does it have something to do with me declaring and assigning the global variables at the top of the class?

  3. #23
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: Strings are not printing as they should?

    Quote Originally Posted by Norm View Post
    Does the code set the current counters to zero
    or is a new instance of a class created with the counters set to zero?

    For either case, the debug printlns should show you what is happening.
    Fof the first case: print out a message every time the values of the counters are changed showing the values of the counters.
    For the second case: print out a message every time a new instance of the class is created with zero values.

    The print out will show you why and where the counters are being reset to zero.
    OH. WAIT UP. Is this the reason why the counters are getting set back to zero:

    In the player class, I create a new object for the categories every time. Does this set all the values back in the category class back to zero every time?

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Strings are not printing as they should?

    every time the program executes the class, the counters get set to zero again
    Methods are executed, not classes. Every time a new instance of a class is created, all the instance variables in the class are given values.

    The code needs to create only ONE instance of the class and reuse it as needed to preserve its contents. If a new instance is created, the values in the old instance are not copied to the new instance.
    If you don't understand my response, don't ignore it, ask a question.

  5. #25
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: Strings are not printing as they should?

    How can I go about making sure these objects are only created once? I've looked into singletons but I am unaware of how I can use them specifically for the category class...

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Strings are not printing as they should?

    Create the instance one time and pass the reference to that instance to all the classes and methods that need it.

    Using singletons would prevent your code from accidentally creating more than one instance. The same effect could be achieved by printing messages during debugging to see if a contstructor is called more than one time.

    Using singletons would remove the need to pass references to the object. That might work. Let's see what the other posters say on that.
    Last edited by Norm; 06-15-2014 at 07:59 PM. Reason: added comment on using singletons
    If you don't understand my response, don't ignore it, ask a question.

  7. #27
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: Strings are not printing as they should?

    Where exactly would I create the instance? And how would you pass an object to a method?

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Strings are not printing as they should?

    how would you pass an object to a method?
    Java Code:
    AClass refToClass =  new AClass(); //  create an instance
    ...
    refToAnotherClass.aMethod(refToClass); //  pass reference to AClass to aMethod
    If you don't understand my response, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Printing formatted strings to laser printer
    By alan2 in forum New To Java
    Replies: 31
    Last Post: 04-05-2013, 10:40 AM
  2. Printing 5 Strings per line
    By mafro in forum New To Java
    Replies: 14
    Last Post: 05-04-2012, 11:03 AM
  3. Replies: 0
    Last Post: 12-26-2011, 04:52 PM
  4. printing string backwards and printing every other
    By droidus in forum New To Java
    Replies: 22
    Last Post: 03-10-2011, 10:17 AM
  5. printing an "E" out of asterisks via strings
    By hokieman07 in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 06:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •