You are viewing the article How to Call a Method in Java at Lassho.edu.vn you can quickly access the necessary information in the table of contents of the article below.
This article is co-authored by a team of editors and trained researchers who confirm the accuracy and completeness of the article.
The wikiHow Content Management team carefully monitors the work of editors to ensure that every article is up to a high standard of quality.
This article has been viewed 9,712 times.
When starting to program with Java, there are many concepts that you need to grasp. Those concepts include class, method, exception, constructor, variable, and so on. In order not to be overwhelmed, it is best to learn little by little. This wikiHow teaches you how to call a method in Java.
public static void methodName () { System . out . println ( "This is a method" ); }
- Public: By putting the “public” access modifier in front of the method name, we can call this method from anywhere.
- Protected: The “protected” access allows the method to be called only in the scope of the base and subclasses.
- Private: If access is declared
private
, the method can only be called from within the class. This access is called default or plan private. This means that only classes in the same package can call this method.
- If the “static” keyword is not used, the method can only be called through an object. For example, if the class “ExampleObject” has a constructor (used to create an object), we can create a new object by typing “ExampleObject obj = new ExampleObject();”, and call the method with the following command. : “obj.methodExample();”.
- If you want a method to return something, just replace the word “void<” with the data type (primitive or reference) of the object or primitive type you want to return. Primitive data types include int , float , double , and so on. Then add “return” along with the object of that data type at the end of the method’s code.
- When calling a method that returns a certain value, you can continue to use that value. For example, if the “someMethod()” method returns an integer, you can set that integer to the value returned by code: “int a = someMethod();”
public class className { public static void methodName (){ System . out . println ( "This is a method" ); } public static void main ( String [] args ) { methodName (); } }
public class myClass { public static void sum ( int a , int b ){ int c = a + b ; System . out . println ( "The sum of A and B is " + c ); } public static void main ( String [] args ) { sum ( 20 , 30 ); } }
Advice
- After calling a method and having a return value, you can call another method based on the value just returned. For example, if
getObject()
method returns an object, that means in theObject
class there is a non-static methodtoString
with the valueObject
returns whenString
is called. So if you want to get thisString
value fromObject
returned bygetObject()
in one line, write ”String str = getObject().toString();
“.
Warning
- Note about abstract classes and methods. An abstract method cannot be used until it is implemented by another class. This is because the abstract method has no implementation code. Abstract classes are used as a kind of framework.
This article is co-authored by a team of editors and trained researchers who confirm the accuracy and completeness of the article.
The wikiHow Content Management team carefully monitors the work of editors to ensure that every article is up to a high standard of quality.
This article has been viewed 9,712 times.
When starting to program with Java, there are many concepts that you need to grasp. Those concepts include class, method, exception, constructor, variable, and so on. In order not to be overwhelmed, it is best to learn little by little. This wikiHow teaches you how to call a method in Java.
Thank you for reading this post How to Call a Method in Java at Lassho.edu.vn You can comment, see more related articles below and hope to help you with interesting information.
Related Search: