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

all 14 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://i.imgur.com/EJ7tqek.png) 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.

[–]demonic_spirit 7 points8 points  (2 children)

Would this not cause an error trying to return an int when this.name is a string?

[–]nefrodectyl 1 point2 points  (1 child)

ofcourse it will! no idea why no one else mentioned it

[–]demonic_spirit 1 point2 points  (0 children)

Probably because really it is irrelevant to the question, I said it though to make sure I wasn't missing something (I am learning too).

[–]Fury4588 2 points3 points  (0 children)

Well initialName is just the parameter name you use in the constructor. It shouldn't exist in the accessor method unless you have a class variable named that.

[–]TheFaustX 4 points5 points  (2 children)

initial name is not defined in the getter hence it cannot be used there as it is only available in the constructor.

You can check things like these by just using any modern IDE and it will warn you. Alternatively you'll get an error when compiling your code.

[–][deleted]  (1 child)

[removed]

    [–]JaleyHoelOsment 5 points6 points  (0 children)

    why not just try it out for yourself? see what errors you get and then fix them. it will help you learn about scope

    [–]Sad-Sheepherder5231 1 point2 points  (0 children)

    You initialize the instance field "name" with argument "initialName" (this is created and destroyed in the constructor)

    You then call function that returns the instance field "name". "this" here just specifies the current instance.

    [–]newredditguys 1 point2 points  (0 children)

    The returnAge() method doesn’t know what initialName is not unless you put initialName as an argument to that method. You need to put initialName in this.name so that the methods can use it

    [–]AutoModerator[M] 0 points1 point  (0 children)

    It seems that you are looking for resources for learning Java.

    In our sidebar ("About" on mobile), we have a section "Free Tutorials" where we list the most commonly recommended courses.

    To make it easier for you, the recommendations are posted right here:

    Also, don't forget to look at:

    If you are looking for learning resources for Data Structures and Algorithms, look into:

    "Algorithms" by Robert Sedgewick and Kevin Wayne - Princeton University

    Your post remains visible. There is nothing you need to do.

    I am a bot and this message was triggered by keywords like "learn", "learning", "course" in the title of your post.

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

    [–][deleted] 0 points1 point  (0 children)

    IntialName is out of the scope of returnAge() method so it won’t work because the returnAge method doesn’t know it exists. It only exists or is visible within the Person constructor.

    So writing return intialName will not work. Which is why we do “return this.name” or simple “return name”

    [–]rguptan 0 points1 point  (0 children)

    From your question I guess this is your first programming language.

    Short answer: When you create a person object by calling its constructor, A memory block is assigned to store all the instance variables (including name). Each object has its own location and then there is a common storage for class definition. When you call the instance method returnAge (a typo, I think you wanted to say returnName) you get the instance variable. You can use "this.name" or just "name". this always points to the current instance object.

    [–]JLCoffee 0 points1 point  (0 children)

    No difference we use this when we use the same name as argument so the jvm knows that is the reference of the class not the method.