简体   繁体   中英

getter and setter method not working as it should

In this program which i am posting, when i call the (setter function) ie. obj.setsize(res) in the main function than it is not able to change the value of(declared variable "size" in the class "contact_entry") private int size=2 to size=1.

The value remains 2 when i call the (getter function) ie. obj.getsize() in the main program, In the if(obj.getsize()>0).when the set method is called than it should set the value of the variable "size to 1" but it is not able to modify the value and the value of "size remains unchanged".

Please let me know the problem i would be thankful.

    import javax.swing.JOptionPane;
    public class nokia {

        public static void main(String[] args) 
        {
            int itr=0;

            while(itr==JOptionPane.YES_OPTION)
            {
                String s3=JOptionPane.showInputDialog("Enter your option\nEnter 1 to add contact\nEnter 2 to delete contact\nEnter 3 to display contact" +
                    "\nEnter 4 to search a contact detail\nEnter 5 to exit");
                switch(s3)
                {
                case "1":
                {
                    contact_entry obj=new contact_entry();
                    if(obj.getsize()>0)
                    {
                        obj.add_contact();
                        int res=obj.getsize()-1;
                        obj.setsize(res);
                    }
                    else
                        JOptionPane.showMessageDialog(null, "MEMORY FULL....NO MORE CONTACTS CAN BE ADDED");
                    break;
                }

                case "2":
                }

            }
        }

    }

    class contact_entry
    {
        private String fst_nme;
        private String lst_nme;
        private long [] mo_no=new long[3];
        private int[] hme_no=new int[3];
        private int size=2;

        public contact_entry()
        {
            fst_nme="ron";
            lst_nme="capton";
            mo_no[0]=mo_no[1]=mo_no[2]=0;
            hme_no[0]=hme_no[1]=hme_no[2]=0;

        }

        public void add_contact()
        {
            fst_nme=JOptionPane.showInputDialog("Enter the first name: ");
            lst_nme=JOptionPane.showInputDialog("Enter last name: ");

            int itr=0,itr1=0;
            for(int i=0;itr==JOptionPane.YES_OPTION;i++)
            {
                if (i>2)
                {
                    JOptionPane.showMessageDialog(null,"NO MORE MOBILE NUMBER CAN BE ADDED...MEMORY FULL");
                    break;
                }
                String s1=JOptionPane.showInputDialog("Enter mobile number: ");
                mo_no[i]=Long.parseLong(s1);
                itr=JOptionPane.showConfirmDialog(null, "Do you want to add addtional contact number");

            }

            for(int i=0;itr1==JOptionPane.YES_OPTION ;i++)
            {
                if(i>2)
                {
                    JOptionPane.showMessageDialog(null, "NO MORE PHONE NUMBER CAN BE ADDED....MEMORY FULL");
                    break;
                }
                String s2=JOptionPane.showInputDialog("Enter phone number: ");
                hme_no[i]=Integer.parseInt(s2);
                itr1=JOptionPane.showConfirmDialog(null, "DO you want to add more contact number");

            }   
        }

        public void display()
        {
            String output= "First Name: "+fst_nme+"\nLast name: "+lst_nme+"\nmobile number1: "+mo_no[0]+"\nmobile number2: "+mo_no[1]+
                    "\nmobile number3: "+mo_no[2]+"\nphone number1: "+hme_no[0]+"\nphone number2: "+hme_no[1]+"\nphone number3: "+hme_no[2];
            JOptionPane.showMessageDialog(null, output);
        }

        public int getsize()
        {
            JOptionPane.showMessageDialog(null, size);
            return size;
        }

        public void setsize(int size1)
        {
            this.size=size1;
            JOptionPane.showMessageDialog(null, size);
        }
       } 

I can't reproduce this with your code. Commenting out add_contact (as it's irrelevant) I see three dialog boxes:

  • 2 (first call to getsize() )
  • 2 (second call to getsize() )
  • 1 (call to setsize() )

So it is setting the value of size to 1.

Note that when you loop round and create a new contact_entry instance, that will have a size of 2... is that what's confusing you?

(By the way, you should really try to follow the Java naming conventions. Your code is very unidiomatic at the moment.)

You are creating new contact_entry object in each loop. So each time you have new object with size = 2 . And you are adding new contact to it. Therefore you are not seeing that increasing contacts in contact_entry object. create that object before the loop.

I guess that what you wan't to do is set a limit on the amount of contacts that can be added.

Now you declared size as an instance variable of contact_entry. For it to work it should be a class variable (so that it is the same for every object).

But please don't do this, because it's just plain ugly design.

What you could do is make a collection on your nokia and put some restrictions on it.

Everytime you make a new contact entry, first check whether the collection is already full and if not then add it to the collection.

该值被设置为1,但是对象obj超出范围,一旦它离开case语句,所以一旦你创建一个新的obj ,它将被重置为“2”,按照默认值,班级本身。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM