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 7789635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:21:25+00:00 2026-06-01T21:21:25+00:00

Can someone please explain output 0 in case of first constructor without this ?

  • 0

Can someone please explain output 0 in case of first constructor without this ?

If the argument variable name is same as class property name and i am using that property inside the method. What does java interpret that “class property” or “the argument variable” ?

Without this:

 public User(int userId){
    userId = userId;

 }

With this:

 public User(int userId){
    this.userId = userId;

 }


 public void PrintUserId(){
    System.out.println(this.userId);
}

 User firstUser = new User(123);
 firstUser.PrintUserId(); 

// 0 without this

//123 with this

  • 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-01T21:21:26+00:00Added an answer on June 1, 2026 at 9:21 pm

    Can someone please explain output 0 in case of first constructor without this ?

    Sure – this statement is a no-op:

    userId = userId;
    

    It just assigns the variable of the userId parameter to itself. It doesn’t touch the field at all. Within the method, the parameter userId shadows the field called userId – so you have to explicitly say that you want to refer to the field, which is what the second version does:

    this.userId = userId;
    

    I’d expect any modern IDE to highlight the no-op assignment in the first version with a warning.

    (It’s worth being clear about terminology, by the way – an argument is a value provided to a method; a parameter is the variable which is declared as part of the method signature. Likewise it’s a field rather than a property.)

    EDIT: If the parameter has a different name, e.g.

    public User(int id) {
        userId = id;
    }
    

    then the parameter doesn’t shadow the field, and the identifier userId still refers to the field. It’s all a matter of working out what the meaning of an identifier is – in your first example, the simple name userId refers to the parameter, which is what causes the problem.

    EDIT: From section 6.4.1 of the JLS:

    Some declarations may be shadowed in part of their scope by another declaration of the same name, in which case a simple name cannot be used to refer to the declared entity.

    …

    A declaration d of a field or formal parameter named n shadows, throughout the scope of d, the declarations of any other variables named n that are in scope at the point where d occurs.

    So in this case d would be the declaration of the formal parameter userId, and the scope of d is the constructor – so throught the constructor, the parameter shadows the field.

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

Sidebar

Related Questions

Can someone please explain why this program outputs 0x00000004? class AndAssignment { static void
Can someone please explain me this peculiar output: #include <stdio.h> typedef struct node {
Can someone please explain what the & does in the following: class TEST {
Can someone please explain to me why this simple assignment doesn't work. Here is
Below code gives the output as abc. Please can someone explain why its not
Can someone please explain this to me? I have the following code: <form action=<?php
Could someone please explain why this happening: class Apple { String type; setType(){ System.out.println(inside
Can someone please explain why this JavaScript code outputs zero instead of one? Also,
Can someone please explain why I'm getting this error? My code: def x(n): if
can someone please explain what's the exports variable for: copied from backbone.js, I also

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.