S
shoa
Hello
I have simple class Student like this:
-----------------------------------------
class Student{
String studentName;
int studentID;
String studentAddress;
public Student (String studentName, int studentID, String
studentAddress){
this.studentID = studentID;
this.studentAddress = studentAddress;
this.studentName = studentName;
}
public String getStudentName(){
return this.studentName;
}
}
-------------------------------------------
Now I create a student object:
Student aStudent = new Student ("John", 122121, "USA");
I want to have the name of this student Object, so I have two methods:
//first method
String studentName= aStudent.studentName;
//another method
String studentName = aStudent.getStudentName();
In a simple application like this, two results as the same. I know that the
first method is not correct. Could you please tell me the differences
between two methods and why I should choose method 2.
Many thanks
S.Hoa
I have simple class Student like this:
-----------------------------------------
class Student{
String studentName;
int studentID;
String studentAddress;
public Student (String studentName, int studentID, String
studentAddress){
this.studentID = studentID;
this.studentAddress = studentAddress;
this.studentName = studentName;
}
public String getStudentName(){
return this.studentName;
}
}
-------------------------------------------
Now I create a student object:
Student aStudent = new Student ("John", 122121, "USA");
I want to have the name of this student Object, so I have two methods:
//first method
String studentName= aStudent.studentName;
//another method
String studentName = aStudent.getStudentName();
In a simple application like this, two results as the same. I know that the
first method is not correct. Could you please tell me the differences
between two methods and why I should choose method 2.
Many thanks
S.Hoa