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?
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?