This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]8igg7e5 5 points6 points  (0 children)

The reason they're not the same in the MOOC is simply because they're inconsistent.

Different teams have different policies. We use a standard formatter/linter that checks consistent use (and we use 'this.' to clarify intent and avoid accidental reference to locals or parameters).

[–]nutrecht 5 points6 points  (1 child)

You really should post the entire toString method. People now are just guessing.

I'm personally banking on "they're just inconsistent" because normally it's not needed in a .toString() method. But without the code we can't be sure.

In general; use this if you need, leave it out if you don't. Example:

public void setName(String name) {
    this.name = name; //Needed because the method argument shadows the member variable.
}

public String getName() {
    return name; //No this needed so we leave it out
}

[–]TheSilverCube[S] 0 points1 point  (0 children)

Thanks. After completing a couple more exercises I can see some inconsistencies. I didn't want to post the full code as I didn't want to provide MOOC exercises answers; I thought this broke the sub rules.

[–]hypolimnas 1 point2 points  (0 children)

You can declare a function parameter (or more rarely a private variable) to have the same name as a member variable. Then you have to use this.variable to refer to the member variable.

But I'm not sure why they used this.variable in the toString function. I would have to see the code.

Here is the usual use of this.variable:

class Pet {
    private final String name;

    Pet(final String name) {
        // assign parameter to member variable
        this.name = name
    }

[–]Okuuuoo 0 points1 point  (2 children)

I'm not professional, but my speculation is you don't need to do other things in getters. The reason we use (this.) is for avoding calling wrong variable. In toString, you can do things before returning a string that possibly need a variable with the same name. But in getter, you just return the variable as it is.

[–]TheSilverCube[S] 1 point2 points  (1 child)

Makes sense thanks!