Posts Tagged define

Java Tutorial 5 – Creating methods

We have used system defined methods like println() and nextInt(), Now let’s create our own method and use it.

Defining method is very useful when bunch of code is getting repeated,  just put that code in a method and just call that method instead of writing all the code again

create a class and add this code as always we do.

  1. Why int and Scanner before main method? 

    if we create int inside the main method, then, only main method can use or change that int. so defining anything in the class and outside any method is called Global declaration.

  2. Why Static?
    to use that int into the main or any other method because main itself is static so any variable used within it must be static and thats why we also crated all the methods static.

We created 3 methods and it can be defined anywhere in the class.Observe the out put and you will dig out everything.

u can create different methods of adding subtracting etc. and create  calc.

Doubts invited,

Have a nice day, Peace.

, , , , , , ,

Leave a comment

Design a site like this with WordPress.com
Get started