Sign in with
Sign up | Sign in
Your question
Closed

Simple Java Question

Last response: in Apps General Discussion
Share
September 28, 2011 3:38:00 AM

Hi guys, i'm trying to teach myself java and i'm doing pretty well...until hit objects...my god am i confused. I've reread the section like 15 thousand times, and i feel like i understand it now except for 2 things; "get" and "set".

what on earth do these do. I have an example from the book, let me highlight the parts in question:

  1. class student
  2. {
  3. int ID;
  4. String Name;
  5. Date birthday;
  6.  
  7. public student()
  8. {
  9. ID= 1;
  10. Name = "Michael";
  11. birthday = null;
  12.  
  13. }
  14. public student (int s_id, String s_name, Date s_birthday)
  15. {
  16. ID= s_id;
  17. Name = new String (s_name);
  18. birthday = s_birthday;
  19. }
  20. public void setName(String new_name)
  21. {
  22. Name = new_name;
  23. }
  24. public String getName()
  25. {
  26. return Name;
  27. }
  28. public void print ()
  29. {
  30. System.out.println("Name is: " +Name);
  31. System.out.println("ID is: " +ID);
  32. System.out.println("DOB is: " +birthday);
  33. System.out.println ("---------------");
  34. }
  35. }
  36. public class Chapter8
  37. {
  38. public static void main(String[] args)
  39. {
  40. student s1 = new student();
  41. s1.print();
  42. student s2 = new student(1053263, "Robert", new Date(1987));
  43. s2.print();
  44. }
  45. }




So my issue is that i dont understand what the setName and the getName do (line 20 and line 24). i comment them out, and the program still runs fine.

why did the book use setName? why not setID? seems so arbitrary as its not referenced anywhere else.

what is getName for? it does look like it's doing anything except "return Name" which doesnt look like it's doing much.

Any help?

More about : simple java question

a b L Programming
September 28, 2011 6:52:37 AM

In your example the "set" and "get" don't appear to do much. But the point is that in good OO programming you don't directly access internal data of a class but do it all via methods. This allows you to change the structure and implementation of a class without having to change any external references to it.

In you case "set" does very little, but in a more complicated example it might, for example, do some verification of the data and only set the variable if the input is valid. And get, in this case, simply returns the contents of the variable; but it could do more. It could be that the data isn't actually stored but is calculated (e.g. an average) or retrieved from an external database. With setters and getters a program doesn't need to know the implementation details of a class it uses.
Score
0
September 30, 2011 10:13:24 PM

Ijack said:
In your example the "set" and "get" don't appear to do much. But the point is that in good OO programming you don't directly access internal data of a class but do it all via methods. This allows you to change the structure and implementation of a class without having to change any external references to it.

In you case "set" does very little, but in a more complicated example it might, for example, do some verification of the data and only set the variable if the input is valid. And get, in this case, simply returns the contents of the variable; but it could do more. It could be that the data isn't actually stored but is calculated (e.g. an average) or retrieved from an external database. With setters and getters a program doesn't need to know the implementation details of a class it uses.


Oh okay thank you so much!

I guess the example the book was using was just really bad as the get and set dont do anything lol.

I found that when using private values and such, they become necessary.

Now one more quick question haha, as i dont want to open a new thread for this.

if i have a constant;

  1. final int SLOW = 1


which is a part of my object, and then i have method that prints out all the information of that object, how do i get it to print the NAME of the constant rather than the VALUE.

Here is a part of my default constructor

  1. public Fan()
  2. {
  3. speed = SLOW;
  4.  
  5. }


And here is the simple method to print out the value:

  1. public void print()
  2. {
  3. System.out.println("the speed is " + getSpeed());
  4. }


However when i run the program i get;

"the speed is 1" when instead i want to get "the speed is SLOW"

Thank you very much
Score
0
Related resources

Best solution

a b L Programming
October 1, 2011 10:59:08 AM

I think that in this case it would be better to use and enum variable rather than integer constants. Have a look at http://download.oracle.com/javase/tutorial/java/javaOO/...
Share
October 24, 2011 12:17:04 AM

Best answer selected by clydefrog.
Score
0
October 24, 2011 12:45:06 AM

This topic has been closed by Nikorr.
Score
0
!