• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is my understanding of methods correct?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So here is the main class with the main method:



Then another class is created within the same project, which is ok bc we already have a main method in the first one:



Then I can apply the messages in the second code to the first code like so:



Want to know if I understand here. So by calling that method from the second class I take in what was already defined by the user number, namely the num. So a user enters say 3 then that 3 gets passed into the method multiplier through the instantiated info. So that 3 goes through some hoops to get to a specific result?



Why isn't anything put into the parameters of Example() in the line Example info = new Example()?

And if I understand. Essentially we create a main method. Then in another class we make a lot of methods that we then take and apply later on to different things in the main class. So if the user enters something into console we can then want short that number into a method which will contort that number into something else, like multiplying it. So the method is like a series of steps that number passes through?

Also when the method was created, in the parameters it said (int number) so is this is say that whatever we call the method we better make sure whatever the var name we pass in must be attached to an int or else we'll get an error. And the name "number" there is sort of like attributing the name number to whatever we pass into the method. So when we called the method later on we put (num) so then the method sort of renames num to number and shoots num through the method to output the result.


Am I on the right track?

Thanks!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:Want to know if I understand here. So by calling that method from the second class I take in what was already defined by the user number, namely the num. So a user enters say 3 then that 3 gets passed into the method multiplier through the instantiated info. So that 3 goes through some hoops to get to a specific result?

Yes. Specifically, the "hoops" written in its multiplier() method.

Why isn't anything put into the parameters of Example() in the line Example info = new Example()?

Because you're calling its constructor - and since you haven't added one yourself the compiler adds a "no args" constructor.

And if I understand. Essentially we create a main method. Then in another class we make a lot of methods that we then take and apply later on to different things in the main class. So if the user enters something into console we can then want short that number into a method which will contort that number into something else, like multiplying it. So the method is like a series of steps that number passes through?

Yes, but they aren't just any old steps; they'll be whatever steps are coded in the method you call.

Also when the method was created, in the parameters it said (int number) so is this is say that whatever we call the method we better make sure whatever the var name we pass in must be attached to an int or else we'll get an error.

Basically; but you can also pass a literal if you want.

For example, line 8 in your Something class could be:
  int exm = info.multiplier(2);
instead of:
  int exm = info.multiplier(num);

And the name "number" there is sort of like attributing the name number to whatever we pass into the method.

Correct.

So when we called the method later on we put (num) so then the method sort of renames num to number and shoots num through the method to output the result.

Well, that's the effect, but you put it much better previously.

Am I on the right track?

I think so, but try and be a bit more precise with your language.

Compilers don't like woffle, and they'll bark at you if you spell things wrong or mismatch brackets (()) or braces ({}), or miss out ';'s.

Remember the mantra of Master Po: Precision in all things, Grasshopper.

Winston
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be interested in reading this thread Main Pain
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!