'Can anyone tell how can i put getter and setter on the below code, and what exactly getter and setter do

public class perform{

    public static void Main(String args[]) throws IOException 
    {
        perform obj = new perform();
        obj.run();
    }
      public void run() throws IOException 
      {
          String inputfile= "c:/file_adress";
          List<String> field = null;
          String delimeter = ";";
          String line="";
          BufferedReader br = new BufferedReader(new FileReader(inputfile));
          while((line=br.readLine())!=null)
          {
              field = new ArrayList<String>();
              field=Arrays.asList(line.split(delimeter));
              for (String object : field) {

                  System.out.println( "-->" + object + "\n");
              }
          }
      }
      }

now when i try to put getter and setter on this code, by right clickin on the code and then going to source menu. it gives error - "The Operation is not applicable to the current selection. Select a field which is not declared as type variable or a type that declares such fields."

can anyone help me what changes i need to make to add getter and setter and why they are used.



Solution 1:[1]

You can add getters and setters for class variables only. In your code, the class is perform and it has two methods - Main() and run(). All the variables you are using in your code and either in Main() or run(). Hence, those are local method variables, and not class variables.

getter and setter methods allow you to efficiently manage the access of class variables by other external classes. Your local method variables won't be accessed by other external classes. Also, a local variable inside one method cannot be directly accessed in another method, even if it is inside the same class because a local variable is only alive inside the method it is declared.

Why these are needed? Please read the explanation given in this stackoverflow post - what is the point of getters and setters

Solution 2:[2]

Getters & Setters are the procedure used for data hiding. If your class has any class level variable and does not have access to outside environment (as data members will be usually private), then access to that data is provided through getters and setters. For example, if I have a class variable private int id; then access to this variable is provided through getId() and setId() methods.

getters will return the current value of the variable and setters will update value of the variable.

In the above code, you are getting error message, as your class does not have any class level variables. All the variables you declared are local to that method they do not have visibility to the class.

Solution 3:[3]

Getters and setters retrieve and manipulate the encapsulated variables within a class, respectively. Since you have no encapsulated variables, there is no reason to implement them in your code.

Solution 4:[4]

I had encountered the same issue.

Maybe you were not clicking correctly inside the source code area of the class.

You can solve this error by right-clicking on the class name from project explore and select->source->Generate Getters and Setters...

Solution 5:[5]

That's happens mostly because when trying to implement the variables in main method. Try to write the variables outside the main method and generate the getter and setter methods

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Pavan Kumar K
Solution 3 Harbi
Solution 4 Bharat Nemade
Solution 5 Mallika reddy