Once again hey :P! I have a question. Can anyone help me with it?

Discussion in 'Programming' started by Lightcab, May 6, 2015.

  1. Hey everyone it's me Lightcab and today I have another question. I just watched a tutorial of thenewboston and there was something like this-
    Code (Text):
    private String girlName;

        public void setname(String name) {
            girlName = name;

        }

        public String getName() {
            return girlName;
        }
    }
    He didn't tell about it in much detail. So, I want you guys to explain somewhat more about it and what does return do? Looking for your kind reply :).
     
  2. Those are called setters and getters. They do what there name describes and sets the value for the first one and gets the value for the second. The return gives the variable girlName and that's a type of String. I'd suggest reading about methods in java whenever you get the chance.
     
    • Agree Agree x 2
  3. I am currently learning about methods and thanks for the reply dude :).
     
  4. Code (Text):
    public String getName() {
            return girlName;
    }
    The return statement returns the data type defined as the methods return type (String above). This return value can be used in other parts of your code by sections that executed this method. For example:
    Code (Text):
    public int getNumberTwo() {
            return 2;
    }

    public int getNumberThree() {
            return 3;
    }

    public int getNumberFive() {
            return getNumberTwo() + getNumberThree();
    }
     
    Ofcourse the above methods are completely useless as you should just enter 2 and 3 in your code, but you get the idea; you can have a method return any object or primitive.

    Another case is methods that don't return anything, we define this type with "public void MethodName()"; as they return 'void'. You can make these methods return with the return syntax without any code to the right of it;
    Code (Text):
    public void DoesntReturnAnything() {
            return;
    }
     
    #4 fishb6nes, May 6, 2015
    Last edited: May 7, 2015
    • Informative Informative x 1
  5. getters and setters.


    e.g.

    public void getTokens(playerVariable) {
    return getConfig().getInt(playerVariable.getUniqueID().toString() + ".tokens");
    }
    wud get the tokens of that player.

    public void setTokens(playerVariable) {
    getConfig().set(ETC)
    }
    would set the tokens.
     
  6. THANK YOU.
    I thank you for asking what those things do!

    ASK WHAT EVERY LITTLE PIECE OF CODE DOES.

    You have the right mind set, you want to ask what everything does and you need to know what everything does.
    Understanding the code you're righting is #1.

    oh and thenewboston is shit.
    a good teacher will explain what he is teaching you and NOT tell you what to write but rather teach you how to write.

    I wish you luck and if you have anymore questions feel free to come ask us!
     
    • Like Like x 1
  7. Dude you are a great explainer. I finally found the answer of my question :).
     
  8. Understanding each line of code is necessary in my opinion. So, I like to ask anything I seem to be confused in. Do you know any good youtuber and thanks for replying dude.
     
  9. More than any youtuber, I would suggest reading into javadocs and googling misc simple tasks. Find random online tutorials for simple things, modify them, experiment. See if you can take things from other tutorials and merge them into some weird Java lovechild. In Java documentation, you might find a cool-looking function that is simple enough to understand at your level. Instead of following one single linear tutorial, seek the knowledge at your own pace and piece it all together from different sources. The tutorials aren't what teach you, it's your own experimentation and ideas that really cement these concepts in your head.

    Tutorials still have their uses, so don't reject them entirely. They are merely a supplement to learning, not an absolute guide.

    And please, ask questions. A lot. As much as possible.
     
  10. Im kinda doing that. I mean first I watch a video and then I try to make something like that and if I get confused anywhere I search it on google. If I can't find a good answer over there then I post my question over here. I'm reading a book called Headstart Java as well and I will ask as much questions as I can :p. Thanks for the reply. Another reason why I watch thenewboston is for the idea of what should I learn next :).
     
    #10 Lightcab, May 7, 2015
    Last edited: May 7, 2015