Simple Java Question

Status
Not open for further replies.

clydefrog

Distinguished
Dec 5, 2009
7
0
18,510
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:

[cpp]class student
{
int ID;
String Name;
Date birthday;

public student()
{
ID= 1;
Name = "Michael";
birthday = null;

}
public student (int s_id, String s_name, Date s_birthday)
{
ID= s_id;
Name = new String (s_name);
birthday = s_birthday;
}
public void setName(String new_name)
{
Name = new_name;
}
public String getName()
{
return Name;
}
public void print ()
{
System.out.println("Name is: " +Name);
System.out.println("ID is: " +ID);
System.out.println("DOB is: " +birthday);
System.out.println ("---------------");
}
}
public class Chapter8
{
public static void main(String[] args)
{
student s1 = new student();
s1.print();
student s2 = new student(1053263, "Robert", new Date(1987));
s2.print();
}
}[/cpp]



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?

 

Ijack

Distinguished
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.
 

clydefrog

Distinguished
Dec 5, 2009
7
0
18,510


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;

[cpp]final int SLOW = 1[/cpp]

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

[cpp]public Fan()
{
speed = SLOW;

}[/cpp]

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

[cpp]public void print()
{
System.out.println("the speed is " + getSpeed());
}[/cpp]

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
 
Status
Not open for further replies.