Answers for "getters and setters java"

5

java get set

class Employ
{
  public String name;						//name of the employ
  
  public String getName()					//Get the name
  {
    return name;
  }
  
  public String setName(String newName)		//Set the name
  {
    this.name = newName;
  }
}
Posted by: Guest on January-08-2021
4

getters and setters javascript

let obj = {
  log: ['a', 'b', 'c'],
  get latest() {
    if (this.log.length === 0) {
      return undefined;
    }
    return this.log[this.log.length - 1];
  }
};

obj.log.push('d');
console.log(obj.latest); //output: 'd'
Posted by: Guest on May-20-2020
-1

setter&getter java

public static void main(String[] args) {
  Vehicle v1 = new Vehicle();
  v1.setColor("Red");
  System.out.println(v1.getColor());
}

// Outputs "Red"
Posted by: Guest on May-16-2021
1

js why to use getters and setters

1) Syntax reasons. It’s easier and faster to read code 
created with accessor functions
2) Encapsulation. I can create safer code with accessor functions.
Posted by: Guest on June-02-2021
-1

use of getter setter in java

package gettersetterProgram; 
public class Mensuration 
{ 
  public double x, y, r; 
// Create getter method for each variable. 
  public double getX() { 
    return x; 
  } 
 public double getY() { 
    return y; 
} 
public double getR() { 
   return r; 
} 
// Create setter method for each variable in the class. 
 public void setX(double x) { 
    this.x = x; 
 } 
 public void setY(double y) { 
    this.y = y; 
 } 
public void setR(double r) { 
  this.r = r; 
} 
// Now write the logic of Rectangle and Circle. 
  public double areaRec() { 
    double area = x*y; 
    return area; 
  } 
 public double perRec() { 
    double per = 2*(x+y); 
     return per; 
  } 
 public double areaCircle() { 
    double area = 3.14*r*r; 
     return area; 
 } 
 public double circumCircle() { 
    double circumference = 2*3.14*r; 
    return circumference; 
  } 
 } 
public class MyTest { 
public static void main(String[] args) 
{ 
  Mensuration mens; // Creating reference. 
    men's = new Mensuration(); // Creating object.
 
// Set the values of the variables. 
     mens.setX(20.5); 
     mens.setY(30.5); 
     mens.setR(12.5); 
  
  double areaRec = mens.areaRec(); 
  double perRec = mens.perRec(); 
  double areaCircle = mens.areaCircle(); 
  double circumCircle = mens.circumCircle(); 

  System.out.println("Area of rectangle: " +areaRec); 
  System.out.println("Perimeter of rectangle: " +perRec); 
  System.out.println("Area of circle: " +areaCircle); 
  System.out.println("Circumference of circle: " +circumCircle); 
 } 
}
Posted by: Guest on September-03-2021

Code answers related to "getters and setters java"

Python Answers by Framework

Browse Popular Code Answers by Language