Using getter and setter methods inside a class in my main class

getter and setter method in java example
setter and getter methods for array in java
example programs of getter and setter in java
getter and setter in java javatpoint
getter and setter methods for arraylist in java
getters and setters in java - geeksforgeeks
check the availability of getter/setter of book name attribute
constructor getter and setter in java example

I have to write a program that writes a class called "Employee" that holds information about an employees name and salary. Then I need to use that information inside my main class to set the employee information then get the employee information and print it to the screen, but I have no idea how to access my information inside my "Employee" class to use in my "main" class. Here's what I have in my "Employee" class.

package classwork6_1;

public class Employee {

    private String name;
    private float salary;

    public String getName()  {        
        return name;        
    }

    public float getSalary() {          
        return salary;     
    }

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

    public void setSalary(float salary){
        this.salary = salary;        
    }     
}

In your main class, instantiate Employee, and call the setters:

Employee emp = new Employee();
emp.setName("Matt");
emp.setSalary(50000);

Then just print it using the getters:

System.out.println("Name: " + emp.getName() + ", salary: " + emp.getSalary());

Setter and Getter Methods in Java, He creates his own Main class, where he begins to use the Cat class In reality, all this is easily achieved with special methods called getters and setters. field values, you could have just written the check inside the setter. 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


Just follow these simple steps:

After creating an instance of Employee

Employee employee = new Employee();

You want to set the values using the setters. For example:

employee.setName("John");
employee.setSalary(100f);

After that you can get the values back using the getters. For example:

String name = employee.getName();
float salary = employee.getSalary();

Improve your class by adding a constructor!

In addition, I would include a constructor to your class. For example:

public Employee(String name, float salary) {
    this.name = name;
    this.salary = salary;
}

Now you can do the following to make an instance of your class:

Employee employee = new Employee("John", 100f);

Java Getter and Setter: Basics, Common Mistakes, and Best , In this post, we take a closer look at getter and setter methods in Java, common The following code is an example of a simple class with a private variable and a couple of getter/setter methods: such as changing the value of a variable within a specified range. public static void main(String[] args) {. Accessing data members of a class depends upon the access specifiers of these members. Sometimes there is a necessity to provide access even to private data members. In this case technique of Accessors (getters) and Mutators (setters) are used. We can use Person class from the previous topic to show how you can access data members with different access types:


The Main class should be something like this

import classwork6_1.*;

public class Main {
    public static void main(String[] args) {
        Employee  e=new Employee();
        e.setSalary(2000f);
        e.setName("Gagan");
        System.out.println("Name: " + e.getName() + " Salary : " + e.getSalary());
    }
}

Getters and Setters in Java Explained, For each instance variable, a getter method returns its value while a setter method sets public class Vehicle { private String color; // Getter public String Once the getter and setter have been defined, we use it in our main: The public setSalary() method takes a parameter (s) and assigns it to the salary attribute (salary = s). The public getSalary() method returns the value of the private salary attribute. Inside main(), we create an object of the Employee class. Now we can use the setSalary() method to set the value of the private attribute to 50000.


how to access my information inside my "Employee" class to use in my "main" class.

  1. Instantiate an Employee object.
  2. Invoke methods on that object.

Here is a complete example of your class. See the public static void main method to see how steps 1 & 2 above work. Other correct Answers on this page show this as well.

By the way, in real work, never use float/Float or double/Double for money where accuracy matters. Floating-point technology trades away accuracy for speed of execution. The BigDecimal class is the opposite, slow but accurate.

// Example app for Answer in Stack Overflow: https://stackoverflow.com/a/53107000/642706
// Simple example, not meant for use in production.
// Caution: *Not* thread-safe.
public class Employee {

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Employee {
    private String name;
    private BigDecimal salary;

    public Employee ( String name , BigDecimal salary ) {
        this.setName( name );
        this.setSalary( salary );
    }

    public String getName ( ) {
        return name;
    }

    public void setName ( String name ) {
        // … add your data validation checks here. Example: Strings that are too short or too long.
        this.name = name;
    }

    public BigDecimal getSalary ( ) {
        return salary;
    }

    public void setSalary ( BigDecimal salary ) {
        // … add your data validation checks here. Example: Numbers that are negative, too big, or too small.
        salary.setScale( 2 , RoundingMode.HALF_EVEN ); // Round to the penny, using Banker’s Rounding.
        this.salary = salary;
    }

    public void giveRaise ( int percentagePoints ) {
        BigDecimal percentageAsDecimalFraction = new BigDecimal( percentagePoints ).divide( new BigDecimal( 100 ) );
        BigDecimal mulitiplier = new BigDecimal( "1" ).add( percentageAsDecimalFraction );
        BigDecimal newSalary = this.getSalary().multiply( mulitiplier ).setScale( 2 , RoundingMode.HALF_EVEN );  // Round to the penny, using Banker’s Rounding.
        this.setSalary( newSalary );
    }

    @Override
    public String toString ( ) {
        return "Employee{ " +
                "name='" + this.getName() + '\'' +
                " | salary=" + this.getSalary() +  // In real work we would *not* be dumping sensitive private data like salary that might end up in logs or other insecure data-sinks. 
                " }";
    }

    public static void main ( String[] args ) {
        Employee x = new Employee( "Alice" , new BigDecimal( "1234.56" ) );
        Employee y = new Employee( "Bob" , new BigDecimal( "678.12" ) );

        System.out.println( x );
        System.out.println( y );

        x.giveRaise( 10 );
        y.giveRaise( 5 );

        System.out.println( "After raises." );
        System.out.println( x );
        System.out.println( y );
    }
}

When run.

Employee{ name='Alice' | salary=1234.56 }

Employee{ name='Bob' | salary=678.12 }

After raises.

Employee{ name='Alice' | salary=1358.02 }

Employee{ name='Bob' | salary=712.03 }

Java Encapsulation and Getters and Setters, The get method returns the variable value, and the set method sets the value. Syntax for both is that they start with either get or set , followed by the name of the public class Person { private String name; // private = restricted access // Getter public class MyClass { public static void main(String[] args) { Person myObj  The get method returns the value of the variable name. The set method takes a parameter (newName) and assigns it to the name variable. The this keyword is used to refer to the current object. However, as the name variable is declared as private, we cannot access it from outside this class:


Java Getter and Setter Tutorial, How to write getter and setter methods in Java with in-depth description, various The following code is an example of simple class with a private variable and a couple of getter/setter methods: manner, such as changing value of a variable within a specified range. public static void main(String[] args) {. Age can't be negative! Smudge's age: 5 years Inside the setter, we created a restriction that protected us from the attempt to set invalid data. Smudge's age wasn't changed. You should always create getters and setters. Even if there are no restrictions on what values your fields can take, these helper methods will do no harm.


Java: Inner classes, Quick summary * an inner class is a class inside of a class * inner classes are The inner classes has access to the enclosing class's instance variables and methods. Likewise, the enclosing class can create a new object using the inner class public class App { public static void main(String[] args) { Robot myrobot = new  @DanielKaplan: the point is that calling getters and setters is in most cases the right thing to do, and only in specific situations not. However, in real world code, when you change an existing getter/setter-implementation later to introduce some intentional side effects, you will most probably have to check every call to the getter or setter inside the class, and also every direct access to


Should the methods of a class call its own getters and setters , I'm not saying that you should eliminate setters on classes with logic entirely. Yes, the methods of your class should call the getters and setters. the private sections of your code (from within the class of course), but you are