• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

Properties in Java

March 21, 2022 Java

In this post, you will learn how to work with properties so that you can use that same knowledge and thus evolve.

  • the properties (get and set) are members that provide a flexible mechanism for reading, writing or calculating the value of a particular field;
  • they allow the data to be easily accessed and also help to promote the security and flexibility of the methods;
  • it is also important to know that the properties allow a class to expose a public way to obtain and set the value by hiding the implementation code or verification;
  • properties that do not implement a set are read-only.
package com.caffeinealgorithm.programminginjava;

public class Main {
  public static void main(String[] args) {
    var person = new Properties();

    person.setFirstName("Nelson");
    person.setLastName("Silva");
    person.information();
    System.out.printf("Age: %d", person.getAge());

    /*
      Name: Nelson Silva
      Age: 25
    */
  }
}
package com.caffeinealgorithm.programminginjava;

public class Properties {
  private String firstName = "", lastName = "";
  private int age = 25;

  public void setFirstName(String firstName) {
    if (firstName != "")
      this.firstName = firstName;
    else
      System.out.println("The string referring to the first name cannot be empty.");
  }

  public void setLastName(String lastName) {
    if (lastName != "")
      this.lastName = lastName;
    else
      System.out.println("The string referring to the last name cannot be empty.");
  }

  public int getAge() {
    return age;
  }

  public void information() {
    System.out.printf("Name: %s %s\n", firstName, lastName);
  }
}

Don’t forget to watch the video and you can always read this post in Portuguese.

Happy coding!

Previous Next

Categories

  • Announcements
  • Python
  • C#
  • Java
  • Go

Latest Posts

  • for Loop in Go
    May 5, 2022
  • Switch in Go
    May 4, 2022
  • Comparison Operators in Go
    May 3, 2022
+351913416022
contact@nelsonsilvadev.com
Porto, Portugal
Nelson Silva © 2022 All Rights Reserved