Java Tutorial [#06] - Methods and return statements

in #programming7 years ago

Header.jpg

Disclaimer!
If you find any mistakes in this post or you don't know what to do at a specific point? Feel free to write me a comment! 👍
Greets from Germany ✌️

Last Part Next part
<- #05 First coding challenges Coming soon ->

In this part of the Java Tutorial, I will talk about methods and their return statements. Let's start with the really basics:
The public static void main(String[] args){ } code, where you write all your code that will be executed, is actually a method. The method got the following attributes, it is public, it is static, it is called main, you can give it a string list of arguments called args.

So lets keep this format and change some parts, let's change name main to myMethod and remove the (String[] args) part and just let the braces empty (). Now our method looks like this:
public static void myMethod(){ } (Of course it shouldn't stand in the main method. Write it under your main method)

And now lets give this method some code, like


public static void myMethod(){
System.out.println("I'm called from a method!");
}

Now write in your main method just the name of your method with empty braces at the end and then run the code -> myMethod();

Our code should look like this:


public class Main{ public static void main(String[] args){ myMethod(); } public static void myMethod(){ System.out.println("I'm called from a method!"); } }

Now run this and the output should be "I'm called from a method!"

Last Part Next part
<- #05 First coding challenges Coming soon ->

Remember: I'm not a genius too! I'm 16 years old but I know quite a lot, because Java or programming in general is fascinating me so much (and yes I have friends and other hobbies :)). So don't give up! Follow you dreams and in some day, they will get true.
Sort:  

Congratulations @theorigin! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @theorigin! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!