+ 1

Why java supports member methods only?

what are member methods?

12th Mar 2018, 4:49 PM
venkat naidu
venkat naidu - avatar
1 Réponse
+ 1
Your first question (title) has to be more specific but I can answer your second question under it. Member methods/functions are blocks of code that is declared in a class (in java at least) which can be executed as many times as you want meaning that you can reuse it making code more readable but also easier to program. On top of that methods can also return a value making them even more useful but doesn’t need to which is why my example has ‘void’ in the method declaration. Last thing methods can do is take arguments which are values the method can use inside itself (these are put in the brackets). public class Program { public void myMethod() { System.out.println(“This is my method”); } } Lastly there are 2 types of member methods: class methods and instance (object) methods. Instance methods are methods that belong to an object and you need to create one before using this method like so... Program obj = new Program(); obj.myMethod(); A class method on the other hand can be accessed without making an object. The method must be declared ‘static’ before this can happen... public static int myMethod() { return 45; } // inside the main method or somewhere else Program.myMethod(); // Program is the class Both types have their advantages and disadvantages. Sorry if this sounds complicated.
12th Mar 2018, 6:17 PM
TurtleShell
TurtleShell - avatar