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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:00:26+00:00 2026-05-25T22:00:26+00:00

Though I’m trying to understand why this is needed, I’m very confused about its

  • 0

Though I’m trying to understand why “this” is needed, I’m very confused about its purpose. For instance, I coded the following:

public static void main (String args[])
{
    SandboxClass1 temp = new SandboxClass1(1,2,3);  
    System.out.println(temp.getX());
    System.out.println(temp.getY());
    System.out.println(temp.getZ());

    System.out.println("----------------------------");

    SandboxClass1 temp2 = new SandboxClass1(4,5,6);
    System.out.println(temp2.getX());
    System.out.println(temp2.getY());
    System.out.println(temp2.getZ());
}

public class SandboxClass1 
{

private int x = 1;
private int y = 1;
private int z = 0;

public SandboxClass1(int x, int y, int zz)
{
    this.x = x;
    this.y = y;
    z = zz;
}

public int getX()
{
    return(this.x);
}

public int getY()
{
    return(this.y);
}

public int getZ()
{
    return(this.z);
}

}

Why do I need to code “this.z = zz”
when I could just as well write, “z = zz”?

  • 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-05-25T22:00:27+00:00Added an answer on May 25, 2026 at 10:00 pm

    In your SandboxClass1 constructor, you have two pairs of variables each called x and y. There’s the x and y declared on the object itself (“private int x = 1”), and the separate x and y that are parameters to the constructor (“int x”).

    The local (parameter) variable shadows the class variable. So if in the constructor you just did

    x = x;
    

    the assignment would have no effect.

    The keyword this is a reference to the object that the method/constructor was called on. In the statement

    this.x = x;
    

    you’re using it to assign to the other x at class level. By qualifying the name, you can tell them apart.

    It’s not necessary to use this with the z/zz assignment because they have different names.

    It’s also not necessary in the getX/Y/Z methods because there are no local variables in those methods shadowing the relevant class variables. It does no harm though.

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

Sidebar

Related Questions

Though I am not very new to Java, the following questions just confused me
I though I understood this, but I can't get it to work: I'm trying
Though .htaccess, I want to redirect /page/var1/var2 to ./page.php?var1=var1&var2=var2. This is very easy BUT
Though the question is basic, but I found it very crucial to understand to
Though it's on the edge of programming questions, I think this is still relevant
though I have visited this site many times, this is my first question. After
though the code is a little long, most of them are very simple IMO.
I though I had a good understanding of how C++ worked but I'm confused
Though this question is asked previously but their is no workaround suggested. I have
Though it may sound a bit silly,still I'm asking this conceptual and logical(as far

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.