get property of a class

Discussion in 'Java' started by shoa, Jul 12, 2005.

  1. shoa

    shoa Guest

    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
     
    shoa, Jul 12, 2005
    #1
    1. Advertising

  2. shoa

    Rod Guest

    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.

    shoa wrote:
    > 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
     
    Rod, Jul 12, 2005
    #2
    1. Advertising

  3. shoa

    Alan Krueger Guest

    shoa wrote:
    > 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.
     
    Alan Krueger, Jul 12, 2005
    #3
  4. shoa

    Carl Guest

    Rod wrote:
    > 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.
     
    Carl, Jul 12, 2005
    #4
  5. shoa

    shoa Guest

    Thank you all

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


    "Carl" <_Nospam_@_DO_NOT_.USE> wrote in message
    news:7gVAe.909$...
    > Rod wrote:
    > > 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.
     
    shoa, Jul 13, 2005
    #5
  6. shoa

    Alan Krueger Guest

    shoa wrote:
    > 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.
     
    Alan Krueger, Jul 13, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,842
    Thomas Weidenfeller
    Oct 12, 2005
  2. christopher diggins
    Replies:
    16
    Views:
    769
    Pete Becker
    May 4, 2005
  3. manuel

    To get all property of a class?

    manuel, Jun 29, 2003, in forum: Python
    Replies:
    6
    Views:
    1,009
    Irmen de Jong
    Jun 30, 2003
  4. Joseph Turian
    Replies:
    5
    Views:
    607
  5. Spam Catcher
    Replies:
    4
    Views:
    161
    Darko
    Nov 21, 2007
Loading...

Share This Page