This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When a methods parameters are set up how does one go about defining those methods?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello very new to Java programming

I want to understand how to define methods when you are given parameters and given a certain task to mutate those parameters accordingly.

Say I am given something like:
public static aMethod(int a, int b, String s, Boolean b){
}

How exactly does one start applying things or customizing those parameters parts? Also after that how do you go about defining a method? I understand that you have "aMethod()" and the parens could be empty, and therefore no parameters but you can still define a method. So you have the choice of throwing in a list of parameters or none at all. If you do choose to customize the parameters, later on we can pass in arguments, like values and string or other things. But how exactly does that work, could someone please provide an example code of this in action. And then how exactly does the dot operator work, it is like once we create a method and some statements between it's brackets we can then call it later on provided it's w/i scope?

Please clear up this confusion.

Thank you all in advance!
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the biggest problem I'm having in understanding what you mean is, what do you mean by "customizing parameters"?

Also, the method skeleton you wrote isn't going to work. It needs a return type:

If the method doesn't return a value, it's return type is void:

As a very simple illustration, say you wanted to create a method that added two integers:

(This method doesn't check for out-of-range problems, but no matter.) Now say you what to use this method:

Does that clear anything up? What other questions do you have?
 
Murthy Tanniru
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer to https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

Here is the example. In the address class setAddress method is used to set zip and getZip() method returns zip.

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:I want to understand how to define methods when you are given parameters and given a certain task to mutate those parameters accordingly.

OK, first: you need to be very careful about your terminology:

1. You can "mutate" - ie, change the value of - ANY parameter that is not defined as final inside your method, but this will have NO EFFECT on the value that was passed to the method.

2. You may be able to mutate the object pointed to by a reference-type parameter if the class allows it (and String and Boolean don't) - but this is generally NOT a good idea unless you document your method very well.

Say I am given something like:

That seems highly unlikely to me - particularly as that code won't compile. What seems far more likely is that you are given a real method with a descriptive name, plus a detailed set of instructions on what it is supposed to do.

So why don't you show us that? Then we'll be in a much better position to help you.

You might also find this article worth reading, as it goes a little bit into how to "plan" a method signature.

HIH

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!