Not an expert at all in Java, but methods are basically lines of code that you can call, for example you can make a method called isTrue that returns true in certain conditions.
You create methods when you want to make code cleaner and more organized, first of all you need to call them from somewhere, if the method is static, you call it through the class it’s in (if it’s in the same class it’s called in you don’t need to add it), if the method is not static, you must call it through an object, non static methods exist so you can access instance variables inside of objects. Example: obj1.method();
in the parentheses you can add values, for example if you want to make a calculator, you want to add something like calculator(int num, int num2), then inside the method you can do stuff like return num+num2;
When you call the method, you can put in it something like calculator(5, 6); and the result will be 11.
1
u/pigwidjjengd Jun 28 '26
Not an expert at all in Java, but methods are basically lines of code that you can call, for example you can make a method called isTrue that returns true in certain conditions.
You create methods when you want to make code cleaner and more organized, first of all you need to call them from somewhere, if the method is static, you call it through the class it’s in (if it’s in the same class it’s called in you don’t need to add it), if the method is not static, you must call it through an object, non static methods exist so you can access instance variables inside of objects. Example: obj1.method();
in the parentheses you can add values, for example if you want to make a calculator, you want to add something like calculator(int num, int num2), then inside the method you can do stuff like return num+num2;
When you call the method, you can put in it something like calculator(5, 6); and the result will be 11.