The Artima Developer Community
Sponsored Link

Java Buzz Forum
Setter and getter methods in java example

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Setter and getter methods in java example Posted: Aug 9, 2016 3:26 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Setter and getter methods in java example
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement

  • If we want to access variables inside a class we can access them using object. object.variable_name; and if we want to change the value of variable we will assign using object.variable_name=value;
  • By using constructor we can assign some values to the class variables whenever object is created but this is one time initialization.
  • To assign some value to variable directly assigning value or get value of variable using object   is not recommended. 
  • Actually we need to use methods to perform any task. And to assign and to get values of a object we have setter and getter methods concept in java.
  • Getter and setter methods are like normal methods in java. but to initialize new value and get the value of instance variables we will use use these methods that is the reason behind specialty of these methods
  • We can set as well as get value from variables so these are called setter and getter methods.
  • so declare variables as private to prevent from accessing directly using object of the class. and use get and set methods in java like
  • setXXX() and getXXX  to assign values and access variables . SetXXX() and getXXX() here set and get are naming conventions to be used. where as XXX represent variable names.
  • If we observe this it is pure encapsulation in java.


Set Method  /  Setter method in java:

  • Purpose of Setter method is to set new value or assign new value to instance variable .
  1. Method name should follow naming convention setVARIABLENAME().
  2. It should accept some value as an argument. here method argument should be of type of variable.
  3. It should have a statement to assign argument value to corresponding variable.
  4. It does not have any return type. void should be the method return type.
  5. In order to set some value to variable we need to call corresponding setter method  by passing required value.



  1. package settterandgettermethods;

  2. public class SetAndGet {
  3.  private String name;
  4.  private int id;
  5.  

  6. public void setName(String name) {
  7. this.name = name;
  8. }

  9. public void setId(int id) {
  10. this.id = id;
  11. }

  12. }



Get method / Getter  method in java:

  • Purpose of Getter method is to get the value of the instance variable.

  1. Method name should follow naming convention getVARIABLENAME().
  2. It should not have any arguments.
  3. It should return corresponding variable value.
  4. So return type must be of type of variable we are returning from the method.
  5. In order to get the variable value we need to call corresponding getter method of variable.

  1. package settterandgettermethods;

  2. public class SetAndGet {
  3.  
  4.  private String name;
  5.  private int id;

  6. public String getName() {
  7.  return name;
  8. }

  9. public void setId(int id) {
  10.  this.id = id;
  11. }

  12. public static void main(String args[]){
  13.  
  14.  SetAndGet obj = new SetAndGet();
  15.  String name =obj.getName();
  16.  
  17. }

  18. }


Java program to get and set variable values without setter and getter methods

  1. package settterandgettermethods;

  2. public class SetAndGet {
  3.  
  4.  private String name;
  5.  private int id;


  6. public static void main(String args[]){
  7.  
  8.  SetAndGet obj = new SetAndGet();
  9.  
  10.  obj.name="setting some value";
  11.  obj.id=1;
  12.  System.out.println(obj.name);
  13.  System.out.println(obj.id);
  14. }


  15. }


Output:


  1. setting some value
  2. 1

Java program to get and set variable values with setter and getter methods

  1. package settterandgettermethods;

  2. public class SetAndGet {
  3.  
  4.  private String name;
  5.  private int id;


  6. public String getName() {
  7.  return name;
  8. }

  9. public void setName(String name) {
  10.  this.name = name;
  11. }

  12. public int getId() {
  13.  return id;
  14. }

  15. public void setId(int id) {
  16.  this.id = id;
  17. }


  18. public static void main(String args[]){
  19.  
  20.  SetAndGet obj = new SetAndGet();
  21.  
  22.  obj.setName("java");
  23.  obj.setId(1);
  24.  System.out.println(obj.getName());
  25.  System.out.println(obj.getId());
  26. }


  27. }

Output:


  1. java
  2. 1

Read: Setter and getter methods in java example

Topic: Log4j 2.x XSD is Not Fully Descriptive Previous Topic   Next Topic Topic: Writing Laws Is Quite Like Programming

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use