Advertisement
Sunjaree

Basic concept of Getters&Setters

Nov 17th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. public class Main {
  2.     public static void main(String[] args){
  3.         getter_setter obj1 = new getter_setter();
  4.         getter_setter obj2 = new getter_setter();
  5.  
  6.         obj1.setValue(9);
  7.         obj2.setValue(11);
  8.         System.out.println(obj1.getValue());
  9.         System.out.println(obj2.getValue());
  10.     }
  11. }
  12. ********************************************************************************************************************
  13.  
  14. public class getter_setter {
  15.     private int x;
  16.  
  17.     public void setValue(int x){
  18.         if(x<10){
  19.             this.x = x;
  20.             System.out.println("your value is " + x);
  21.         }
  22.         else{
  23.             System.out.println("The value of x should be less than 10");
  24.         }
  25.     }
  26.     public int getValue(){
  27.      return x;
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement