Are methods in Java algorithms
In this article you will learn everything about the topic Java methods. From the method call to the parameters to the return values and the getter setter methods.
You want everything you need to know about without much effort Java methods learn? Then check out our Videoto do this!
Methods Java simply explained
The Main method have you already met. It also reflects the typical structure of a Java method or function. A Java method is structured as follows
It consists of oneModifier, here, which regulates the access. Then comes the optional keyword, depending on whether you are using the method as a static or do not want to flag statically. If you don't want to make it static, just leave it out. Then you write the Data type of the return value. In our case this is the value, which is an integer here. If there would be no command, you program one instead of the.
Then comes the Name of the method and the parentheses, here. The parameters of the method are in brackets. As you can see from our example, a method does not have to have any parameters. Then you just don't write anything in the parentheses. Parameters and method names together also become the signature called the method. The commands are then in the curly brackets. If the method returns a value, write in followed by the respective variable name. If there is a signature in front of the method, you are not allowed to program. The method shouldn't return anything. It is important here that afterwards the method is left, which is why you always do that to program all commands.
Modifier and Static
Often one of the Modifier,, or used in Java. and have you already met in the video about classes. In summary, it means that you can only access the corresponding method or variable from within the class. is the opposite, you can also access them from outside the class. On the other hand, you can access it from a class in the same Java package or a sub-class. If you do not use a modifier keyword, this is the case and the corresponding program segment can only be accessed from the class or the package.
You also have that in the Main method got to know. We have called static main methods or variables independent of the particular class in which they are written. That again means that not an extra Object of the class must be created before this variable or method can be used. If you omit this, an instance of the class must first be created for the further processing of the respective Java methods or variables.
Call Java methods
For the call of a method or function, we differentiate between two different cases.
- 1st case: Method is in the same class
- 2nd case: Call method of an object.
Let us first consider the 1st case. The following applies to the same class Method call:
The method is called here in the Main method.
Now we come to the 2nd case: For the Calling a method on an object the following applies:
The second Class is:
The signature of the method is important. This can only be unique per class, otherwise the program will not find the method.
As you can see, you are using the 2nd case to call the Java method thePoint operator. In front of this is the object to which the method is to be applied and after it the method with the corresponding parameters.
In the example above we have that as Java method parameters to hand over. So external values can be given to the respective Java methods. In the video about variables you learned that variables in Java must first be declared before you can initialize them. Let's take a closer look at the following example:
With you declare the variable with the data type, or and with the method call you initialize the variables with the corresponding values. It is important here that you stick to the order. This means that if there is an integer number in the second position in the method call, the data type must also be noted in the second position of the method. If there was a, the method call would not work.
Java methods overloaded
In the contribution to the classes we had the case that we overloaded the constructor. With normal Java methods you can do the same by using the same method name but different parameters. Two methods which have the same name have to differ in terms of the signature. That means the parameters must not be the same. Otherwise there will be an error.
Java getter setter
This chapter builds on the article about the Java classes. We'll take a closer look at the here Attributes and how you think about this special Java methods make accessible.
In classes, the attributes are provided with the modifier, since nothing should be changed here from outside the class. They shouldn't be so easily accessible. Nevertheless, you sometimes need a special attribute in the computer code in a different class or want to change it. There are so-called getter and setter Java methods for this.
To show you how these special methods work, we'll build the Great person. It should have a name as an attribute. It should also have a constructor that assigns the name of the class variable. Then comes a setter and a getter method.
Getter and setter Both are methods because you want to access them from outside of a class. It is convention to write a “get” or “set” in front of the name of the method. The name of the method is the same as that Variable names the variable to be handled in the method. In our case, this is how the name and.
The Set method does not return a value to the method call. It only assigns a new value to the class variable. Since there is no return value for the method, you have to write a in front of the method name. It is also important that you pass a parameter of the type with the new name to the method. We call the variable.
The Get method however, it returns the current value of the variable. This is one way of finding out the assignment of the variables. Therefore you cannot program here, you have to write down the data type of the variable. That would be a.
We want to access the class human from a class with the main method. To do this, we first create a human object and assign it the name "Alex".
The first edition will give you the name Alex, the second the name Tom. This is because you create the object with the name Alex, but then use the Set method to change it to Tom.
Check out our class video again to become a real Java pro!
- Which 80s rock band is overhyped
- Is tank war out of date?
- Can someone read the Devi Bhagavatam
- What are pay slips
- What is the derivative of 4x
- Why do people always offer me things
- What is the perfect inelastic demand
- Was George Harrison the most underrated Beatle
- What is nikujaga made of
- How many mountain ranges does Canada have
- How do you manage compliance regulations
- How can i lose 75 pounds
- How successful has SpaceX been to this day
- Where is Tesla going
- What is an Android iPhone
- What does the moon mean
- What is there in banking
- What is considered emo music
- Could you share your chicken soup recipe
- What does fate mean for Islam
- How to make money from home
- What are big truck values
- Is there WiFi on the Jamia Hamdards campus
- What is hyphenation
- How can I do underwater fashion photography
- What is considered emo music
- Who created the school
- 1080p is good for a projector
- Who is the learned person in the world
- What are the symptoms of pneumonia
- How can I get healthy shiny hair
- What are the most beautiful cities in Iran
- Is transmedia storytelling really about the actual storytelling?
- What is the lowest thickness of the plate