Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8635051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:52:40+00:00 2026-06-12T09:52:40+00:00

I have a question regarding this in the following code. In the following, this.name

  • 0

I have a question regarding this in the following code. In the following, this.name will set the name. We could also do this using name = name, so my question is should the this pointer be used. This is not an homework

import java.io.*;

public class Employee{
    String name;
    int age;
    String designation;
    double salary;

    //This is the constructor of the class Employee
    public Employee(final String name){ //EDIT: Made parameter final
        this.name = name;
        //name= name; this is also correct
    }

    //Assign the age of the Employee  to the variable age.
    public void empAge(int empAge){
        age =  empAge;
    }

    //Assign the designation to the variable designation.
    public void empDesignation(String empDesig){
        designation = empDesig;
    }

    //Assign the salary to the variable salary.
    public void empSalary(double empSalary){
        salary = empSalary;
    }

    //Print the Employee details
    public void printEmployee(){
        System.out.println("Name:"+ name );
        System.out.println("Age:" + age );
        System.out.println("Designation:" + designation );
        System.out.println("Salary:" + salary);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:52:42+00:00Added an answer on June 12, 2026 at 9:52 am
      //      name= name; this is also correct
    

    This is not correct. It’ll assign your parameter to itself. By using the this keyword, you’re declaring which name you’re using (i.e. the field on your Employee object).

    You may wish to name the field differently from the parameter. However this means that all works well until someone automatically refactors your code to (perhaps inadvertently) declare the field and parameter as the same name!

    For this reason you’ll often see method signatures defined thus:

    public Employee(final String name)
    

    The final keyword prevents reassignment, and stops you from mistakenly reassigning the input parameter, and consequently not assigning to your field. Note also that if you declare the field as final, then compilation will fail if you don’t make an assignment to that field. Using final is a good way to trap such errors and also enforce the immutability of an object (often a good thing – it contributes to a more reliable solution, especially in a threaded environment).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding animations via javascript in HTML using DOM. In this
another noob question regarding F#. If I have the following code... let ExeC =
EDIT: Optimization results at end of this question! hi, i have a following code
I have a question about passing parameters in Tcl regarding to the following code:
I have a question regarding the following code: abstract class a { public static
Again I have a question regarding this plugin: http://t.wits.sg/2008/06/20/jquery-progress-bar-11/ What I want to achieve
I have this very straight forward question regarding Thread and Timer classes in Java
i have a very conceptual question regarding jquery. i have this fiddle http://jsfiddle.net/zvCrN/ What
This question is related to database/RDBMS where i have little knowledge regarding performance and
I have the following code: http://jsfiddle.net/fCWJ5/1/ , and following doubts regarding the viewbox. body{margin:0;}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.