Results 1 to 2 of 2
  1. #1
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    157
    Rep Power
    5

    Default different instant variable of same class

    Hai all

    is the values are storing statically and why???????????????

    if you run the programme it willl give the output

    Name : Anna
    Name : Beah
    Name : Cris
    Id : 1
    Name : Cris
    Grade : 32.1
    Id : 2
    Name : Cris
    Grade : 32.1
    Id : 3
    Name : Cris
    Grade : 32.1

    see the name and grade is setting as static why????????


    Q 2)
    another programme

    public class Testings {

    public static void main (String[] args) {
    X xx;
    X yy;
    X zz;
    xx=new X();
    yy=new X();
    zz=new X();
    xx.setStudentId(5);
    yy.setStudentId(10);
    zz.setStudentId(15);
    System.out.println (xx.getStudentId());


    System.out.println (yy.getStudentId());


    System.out.println (zz.getStudentId());
    System.out.println (xx.getStudentId());

    }

    }

    class X {

    private int studentId;
    public int getStudentId()
    {
    return studentId;
    }

    /**
    * Changes the value of studentId
    */
    public void setStudentId(int studentId)
    {

    this.studentId = studentId;
    }

    }


    the out put is

    5
    10
    15
    5

    why is like this though the setting is done to a single private int studentId; but done by different instant variable of same class
    i'm confused please explain correctly if any on can

    thanks
    Attached Files Attached Files

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    As far as your first question is concerned, there is nothing static here (except the count). The reason why the student record information is the same for all three students is that ... the same student record was assigned to all three of them!

    The second question is very similar: again the id is not static. So setting it to different values for the three students results in all three having different values.

Similar Threads

  1. Double Variable Class
    By hypes057 in forum New To Java
    Replies: 9
    Last Post: 09-06-2009, 02:06 PM
  2. Replies: 7
    Last Post: 03-16-2009, 07:19 AM
  3. Interface variable to class
    By zill in forum Advanced Java
    Replies: 6
    Last Post: 10-11-2008, 03:29 AM
  4. Instant Messaging Program
    By Zosden in forum Networking
    Replies: 13
    Last Post: 05-05-2008, 07:18 AM
  5. Public class variable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •