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 trial

Java Java Objects (Retired) Meet Objects Privacy and Methods

Aditya Puri
Aditya Puri
1,080 Points

What is the point of making the method?

I don't quite get it.....why did we make the method??

Can't we just access the value through the object? Then why did we make a method which does the same thing? How can the user change the field value?

2 Answers

I don't quite get it.....why did we make the method?? To give the guys who started this java course an idea what methods are.

Can't we just access the value through the object? Of course you could, if the varaible is public. But you dont really do that in praticular work. Just imagine if you have to do complicated big calculations...you want to do all the complicated stuff in the other class and return it via the method. Not in your main class. You dont want to have a main class with 10k code full of compicated functions. Almost every "job" (or part) in your programm should be doneby a certain class which has the methods to do that jobs. Also you can use methods over and over agian.

Then why did we make a method which does the same thing? How can the user change the field value? Because that is not the point of this video. This video should give the user and idea how methods work and why they are used in all the object-oriented languages out there.

These videos are not about copy and pasting code. The viewer should understand what is happening in the code and why the teacher is doing it. There is no point to create 5 methods in this video, it is just an introduction to methods.

Just be a little patient you will get the idea behind methods and OOP in general soon

Aditya Puri
Aditya Puri
1,080 Points

so, like we can do complex calculations regarding class fields and then return them through methods??

class file2{
    public String test(int a) {
        String result = "";
        for(int i=1; i<=a; i++){
            result += "test ";
        }
                if(a > 10000) {
                result = "a is too big");
                }
        return result;
    }
}
file2 file2 = new file2();
file2.test(5); //print test 5 times
file2.test(5000); // print test 5000 times
file2.test(10001); // whooops thats not possible a is too big

So this is a bad example but i just want to show you, that we use methods to (most of the time) return some value we need. We can use some statetment to make condition which value will get returned or make validations if f. e. the user input is valid and so on there is alot of practical use. We make methods to just write code ONCE (dont always work ofc) and use it over and over again. We dont want to write System.out.println("whatever") 5000 times...also we dont write thousands of loops in our main classes. We "outsource" these code in other classes that will do that job for us. These classes get created to do some jobs and when they are finished they might get never changed again. If you build a game you wont write all you game parts in your main class. There is a class for the menu, for saving...a class for enemys, a class for battleling. All of them are part of the game but they do different stuff. If you would try to write all that code in one singe file it would be chaos.

Aditya Puri
Aditya Puri
1,080 Points

oh, so we write some program that will take different arguments, then do something and then return the value. To get the values we call the method, right?

Yeah you could say that methods are some...program parts or sub-programs...that will do a certain job. Or they just do something that we need a lot of time. Lets say a method for printing (agian bad example since we usually have already methods for printing in most langauges). When you write a programm there might be 5 different forms with buttons to print something. You dont want to write 5 times code for printing you just want to write one universal method to do the job. Of course that is not always possible. But it is already worth if the method can do the job in 4 of the 5 forms right :). It will save time for us and the code will be more clear.

Also there are methods who dont have a return value. These methods might just change something. Let's say there is a video game (yes games are easy to explain) and your charakter has a Level-up. Then there is maybe a method who will just change the stats for your charakter. This method might just change the varaibles of your stats (if(level==5){ attack +=1; defense += 1;} and return nothing. But it still did a job and will do it over and over agian.

So methods can do some stuff in your program and return a value or do some stuff without returning a value. Methods who dont return a value have are void methods. You already saw one: public static void main(String[] args)....you main method will run the entire program, of course it can not return a value...since the program finished if main method finished.

int someint = doubleit(2); //You assign the return value of some method that dobules 2
if(isItEven(2)){} // you want to get the bool value for an if statement...the method should check if the number is even or not
while(isItEven(2)){} // same example

Assigning the return value of an method is pretty easy as you can see. You dont even have to assign it as you can see in the statements. Or here:

int sumOfTwoRandomNumbers = randomNumber(1, 10) + randomNumber(1 ,10);
// here we make the addition of return values...since the return values are ints we can sum them
// also you can see that methods can have several statements. Here we want to generate random values from 1 to 10
// and make an addition. The value will then be assigned to sumOfTwoRandomNumbers 

Edit: All these methods are just examples and wont work if you try them (well you could create them :)

Aditya Puri
Aditya Puri
1,080 Points

oh, thanks for the comprehensive explanation :D I now understand

nice answer