get property of a class

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
 
R

Rod

Well ...yeah ...the assignment "String studentName=
aStudent.studentName;"
works, however, normally, one would make the studentName private in the
Student class definition. Once private, the assignment:
"String studentName= aStudent.studentName;" would not work ...

The idea here is to hide, in the Student class, the means / logic
associated with setting the studentName. Normally, one would have a
"setter" method in the Student class to set the value of studentName
....the setter method would be public ...the class variables would be
private.

Rod.
 
A

Alan Krueger

shoa said:
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.

Google for "object-oriented encapsulation" or something.
 
C

Carl

Rod said:
Well ...yeah ...the assignment "String studentName=
aStudent.studentName;"
works, however, normally, one would make the studentName private in the
Student class definition. Once private, the assignment:
"String studentName= aStudent.studentName;" would not work ...

The idea here is to hide, in the Student class, the means / logic
associated with setting the studentName. Normally, one would have a
"setter" method in the Student class to set the value of studentName
...the setter method would be public ...the class variables would be
private.

Rod.

One small note that I would like to add to this is that data hiding is a
product of encapsulation, not the goal.

Imagine you have a data member that is related to the value of your
studentName field like a "File as" name or an initials field. (This is a
trivial example, I know, but is just used as an example). By making the
data member itself private/protected, you can enforce the logic that
will update the dependent fields whenever the name changes. For example:

public void setStudentName(String sn){
this.studentName = sn;
updateInitials(sn);
updateFileAsName(sn);
}

If you allow someone to go around this method and set the member
variable studentName directly, you no longer have control over
synchronizing the values of related/dependent data. In this case, the
initials field may or may not match the appropriate values in the
studentName field.

Again, this is a very trivial example and the concept goes far beyond
data relationships. I don't think it would be very difficult for you to
imagine situations where you may want to perform certain actions or
events when certain sets of data change.

Carl.
 
S

shoa

Thank you all

Also I can check the valid of input value
public void setStudentName(String sn){
if (sn........................
else
 
A

Alan Krueger

shoa said:
Also I can check the valid of input value
public void setStudentName(String sn){
if (sn........................
else

You'll be wanting to get book on Java and/or work through Sun's
tutorials, I think.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top