Results 1 to 4 of 4
  1. #1
    Ujemny is offline Member
    Join Date
    Nov 2013
    Location
    Poland
    Posts
    8
    Rep Power
    0

    Default Saving result in class

    Hello,
    i have big problem. I can't figure out how to solve it(maybe because it is too late ).

    So, I have two classes(bubble sort and insert sort) which are sorting dates. I need to save their comparings and reallocation of elements.
    And i dont know how to do it. The point is if I want save that dates(comparings and reallocation) to Result class i need to create new instances of class in each sorting classes so i can't later do something with that, because I have null in Main class.
    To avoid that problem i have tried to make Result class as singleton.. I mean: "public static final Results INSTANCE = new Results();" and then in classes "static Results result = Results.INSTANCE" without constructor and to save for example comparings i had method:
    Java Code:
    int SetComparings(){
            return comparings++;
        }
    but it do not work too, because it overwrites old dates.

    Is there anyway to do not create instance of class?

    I dont want to copy whole code because it is too long and cant be unreadable, but i will put you structure of my program:

    Java Code:
    class BubbleSort{}
    
    class InsertSort{}
    
    class Results{}
    
    class Sorting{} // class with switch to choose which way of sorting use
    
    public class Main{}
    I hope i explain my problem. Any help will be appreciate!

    Thanks!

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    7

    Default Re: Saving result in class

    Hi,

    Can you give more details about your problem? Right now I didn't understand you.
    (May show more details of your code?)

    Regarding the singleton:
    If you don't create a constructor, the compiler will create a default one. So you should create a private constructor in a singleton.
    You should not make the field public. That way someone could alter it (e.g. set it to null). So a getInstance method is better.
    But that are just generic hints and your problem should be independend to these points.

    With kind regards,

    Konrad

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    29

    Default Re: Saving result in class

    Quote Originally Posted by kneitzel View Post
    Regarding the singleton:
    If you don't create a constructor, the compiler will create a default one. So you should create a private constructor in a singleton.
    You should not make the field public. That way someone could alter it (e.g. set it to null). So a getInstance method is better.
    But that are just generic hints and your problem should be independend to these points.
    Or make the INSTANCE variable a public static final one; that way it can't be fiddled with, but it needs to be initialized by the private constructor at definition time (and the singleton object should be immutable).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Saving result in class

    Or use an enum declaration for your singleton.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. -Infinity result
    By FOX427 in forum New To Java
    Replies: 2
    Last Post: 03-18-2012, 01:15 AM
  2. Replies: 7
    Last Post: 01-01-2012, 12:29 PM
  3. Keep getting result of 0.
    By dookie1293 in forum New To Java
    Replies: 7
    Last Post: 06-09-2011, 05:01 AM
  4. Struts 2 error : No result defined for action / result
    By sameerk in forum Web Frameworks
    Replies: 1
    Last Post: 05-17-2011, 10:15 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
  •