Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24
Like Tree1Likes

Thread: Completely over-thinking my class structure - second eyes, please?

  1. #21
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    93
    Rep Power
    0

    Default Re: Completely over-thinking my class structure - second eyes, please?

    Quote Originally Posted by SurfMan View Post
    I want you to tell me what the output is of this small program:

    Total 1: = ????
    Total 2: = ????

    [/code]
    Total 1 = 10
    Total 2 = 9 because you call the getPrice() method.

    I don't see the large difference between that and mine. I believe I was doing it in a similar manual at the end - using getters & setters.

  2. #22
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,091
    Rep Power
    4

    Default Re: Completely over-thinking my class structure - second eyes, please?

    Quote Originally Posted by mallorz View Post
    Total 1 = 10
    Total 2 = 9 because you call the getPrice() method.

    I don't see the large difference between that and mine. I believe I was doing it in a similar manual at the end - using getters & setters.
    I was just trying to get the point across to use getters and setters when interacting with other classes.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #23
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,358
    Rep Power
    6

    Default Re: Completely over-thinking my class structure - second eyes, please?

    Its a better example than the usual "it allows for thread-safety" argument, that's for sure.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Completely over-thinking my class structure - second eyes, please?

    You should always use getters and setters because it allows you to change the internals without affecting the contract. Once you publish a class which allows someone to assign directly to a field, your committed. You may no longer change how that field is stored internally because your changes could break some existing code. And storing or returning references to mutable classes is especially dangerous because the user still retains a copy and can change the state later on (which I mentioned in my earlier post).

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

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 5
    Last Post: 01-16-2013, 05:32 PM
  2. Soon to graduated, thinking about freelance work
    By knightwriter in forum Jobs Discussion
    Replies: 4
    Last Post: 02-23-2012, 05:38 PM
  3. How to create class from a C structure
    By jcardozo in forum New To Java
    Replies: 8
    Last Post: 12-06-2008, 11:47 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
  •