Note that there are some explanatory texts on larger screens.

plurals
  1. POgetter and setter method not working as it should
    primarykey
    data
    text
    <p>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. </p> <p>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".</p> <p>Please let me know the problem i would be thankful.</p> <pre><code> 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()&gt;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&gt;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&gt;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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload