Java Methods, Classes, and Objects

Ayana Bando
4 min readApr 25, 2021

--

A method is a block of code that only runs when it is called. You can pass data, known as parameters (or arguments), into a method. Methods (or functions) are used to perform certain actions, to help keep code DRY (Don’t Repeat Yourself), and to reuse code; define the code once, and use it many times.

  • thisIsMethod() is an example name of a method
  • static means that the method belongs to the Main class and not an object of the Main class
  • void means that this method does not have a return value.

The main method is the entry point for your application and will subsequently invoke (run) all the other methods required by your program. The main method accepts a single argument: an array of elements of type String.

For now, just remember that every Java program has a class name (more information on that below) that must match the filename and that every program must contain the main() method. Curly braces, {}, mark the beginning and the end of a block of code and each code statement must end with a semicolon (see below for details).

Parameters and Arguments

Information can be passed to methods as parameters. Parameters act as variables inside the method.

Parameters are specified after the method name, inside the parentheses. A method can have many parameters, as long as they are separated with a comma.

The void keyword, as stated above, indicates that the method should not return a value. For a method to return a value, primitive data types, such as int, char, are used. The return keyword must be used inside the method:

public class Main {// Create a cartEmpty() method with an String variable called itemsstatic void cartEmpty(int items) {// If "items" is less than 0, print “Add some stuff”if (items = 0) {System.out.println(“Add some stuff!”);// If cart has items, print how many items are in cart} else {System.out.println(“You have %s items in your cart!”, items);}}public static void main(String[] args) {cartEmpty(10); // Call the cartEmpty method and pass along item count of 10}}

Note: Multiple methods can have the same name as long as the number and/or type of parameters are different.

In Java, variables are only accessible inside the region they are created. This is called scope.

A block of code refers to all of the code between curly braces {}. Variables declared inside blocks of code are only accessible by the code between the curly braces, which follows the line in which the variable was declared:

public class Main {public static void main(String[] args) {// Code here CANNOT use x{ // This is a block// Code here CANNOT use xint x = 100;// Code here CAN use xSystSystem.out.println(x);} // The block ends here// Code here CANNOT use x}}

A block of code may exist on its own or it can belong to an if, while or for statement. With for statements, variables that are declared in the statement itself are also available inside the block's scope.

Recursion is the technique of making a function call itself. This technique provides a way to break complicated problems down into simple problems which are easier to solve.

Use recursion to add all of the numbers up to 10.

public class Main {public static void main(String[] args) {int result = sum(10);System.out.println(result);}public static int sum(int k) {if (k > 0) {return k + sum(k — 1);} else {return 0;}

Just as loops can run into the problem of infinite looping, recursive functions can run into the problem of infinite recursion, where the function never stops calling itself. To prevent infinite recursion, a recursive function should have a halting condition, which is the condition where the function stops calling itself. In the previous example, the halting condition is when the parameter k becomes 0.

More examples can be found in the link below.

Classes and Objects

Classes and objects are the two main aspects of object-oriented programming.

Objects in Java, like objects in real life, have states and behavior; states are stored in fields called variables and their behavior is exposed through methods. A class is a blueprint from which individual objects are created. The Java docs state:

“There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components.”

Class: Fruit

Objects: apple, banana, papaya

Method: be eaten, fall on ground

Class: Vehicle

Object: Car

Method: Drive, brake

For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.

See more information here: https://www.w3schools.com/java/java_classes.asp

https://docs.oracle.com/javase/tutorial/java/javaOO/

--

--

Ayana Bando
Ayana Bando

Written by Ayana Bando

Innovator and collaborator interested in continuing to grow and learn

No responses yet