Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialname564681351614
3,497 PointsWhat makes a method a method?
Within the "Privacy and Methods" section he says that we'll create a method and he types the below:
public String getCharacterName () { return mCharacterName; }
What makes this a method? Is it just that the "()" are present? Is it that the "()" make something a method and the "=" makes is a variable?
I also am not understanding what a member variable is. I Googled this and found "A member variable is a variable that belongs to an object, whereas a local variable belongs to the current scope.". I understand that the "current scope" would be whatever is within the "{}". However, at numerous points it's stated that EVERYTHING within Java is an object. So, wouldn't that scope be an object? If that's true than the above local variable definition doesn't make much sense to me.
3 Answers
Ryan Jin
15,337 PointsA method is a function within a class or struct, possibly enum if enum exists. A function is a set of actions, a variable holds a value. A function has () at the end of it because you can pass arguments to a function to change how the function works, for example, you can have a function that accepts an integer and a string, so the parenthesis should look something like (int aNumber, string aString) You can actually name it whatever you want. A variable only holds a value even though in some programming languages like swift, a variable can hold a function, but basically, a variable holds a value, an int, a string, a boolean, an instance of a class, etc. A variable does not perform any action unless the initialization of that data type includes some actions. But to be clear, functions do things, variables hold things. That's the general idea.
Craig Dennis
Treehouse TeacherInside of a class definition you can have a couple of things:
The definition of variables that will belong to an instance of the class. Remember an instance is created using the new keyword. These are called member variables.
Constructors, these are methods that get called when you want to create a new instance of a class. It looks like a method, but its return type is the name of the class.
Methods. These are what an instance of your class can do. They are the actions. They can perform things on the internal state of the instance. It can change those member variables. They can also return values, so because Java is strongly typed, you need to tell callers of your method what to expect. So really, what is specifically "making" it a method, is it's declaration which is return type followed by method name followed with parenthesis and then the parameters defined followed by a code block in between {.
Does that help?
name564681351614
3,497 PointsThanks Craig. I'm having a hard time absorbing this. Would it be possible to take the code from the exercise and basically take your above text and add that as comments within the code (? I am a VERY visual learner. This would help tremendously.
Ted Sumner
Courses Plus Student 17,967 PointsIn PHP a method is a function contained within a class. I imagine it is the same for Java. You state that everything in Java is an object probably overstates it. You can probably program without classes if you really wanted to.