Learn Java Fundamentals with Donna: Understanding Methods

Do DON Done
4 min readOct 15, 2022

--

You probably have heard that the best way to learn is to teach others. There’s even a term for it called The Protégé Effect. I’m currently learning Computer Science while working full-time at Google as a product specialist. I aim to dedicate some time every weekend to review what I have learned and summarize the concepts into a blog post. I hope this post can be helpful to you while keeping me accountable!

Object-Oriented Programming (OOP) concept in Java

Java is an OOP language. What that means is that when we read the code, we feel like reading a manual in which we can understand the object really well. If we take a glass of water as an example, when we look at a glass of water, we can see the state and behavior of this object. The state, which is referred to as instance fields in Java, tells us that the glass is half empty while the behavior, which is referred to as methods in Java, tells us the capability of the glass which includes storing 100ml of liquid. In this post, we will be looking at the syntax and how methods behave.

Structure of the Java code

Java code has a modular structure. It usually starts with declaring the State of the object (read up on my previous post on Java Class), then defining the Methods and lastly running the Main Method at the end.

I was confused about what the Main Method was in the beginning but I slowly learned that it is the entry point of any java program. Although the Methods are defined, they are not executed until you call them in Main Method. In short, Main Method controls what code should be run. In terms of the syntax, it will always be public static void main (String[] args).

public class Person {// state of an object   int age;// behavior of an object public void set_value() {    age = 20; } public void get_value() {    System.out.println("Age is " + age); }// main method public static void main(String [] args) {// creates a new Person object Person p = new Person();    }}

1. Defining the Methods

public void checkBalance(){
System.out.println(“Hello!”);
System.out.println(“Your balance is “ + balance);
}

In comparison to Python, you’d need to declare everything in Java. In the example above,

  • The name of the method is checkBalance()
  • public means that other classes can access this method
  • void means that this method has no specific output. All methods have outputs so if there’s no specific output, you’d need to add in void. If you don’t include void, you’d need to have a return statement
public int numberOfTires() {
int tires = 4;
return tires;
}

For non-void method, we’d need to add a return statement. Here, we have int to signify that the tires return type is an int. Unlike void methods, non-void methods can be used as either a valuable value or as part of an expression.

If we return an object, we return a reference to the object and has the output of the memory address. Therefore we may need to use toString() method for a class to return a String that will print the subject instead of Store@6bc7c054.

class Car {

String color;

public Car(String carColor) {
color = carColor;
}

public static void main(String[] args){
Car myCar = new Car("red");
System.out.println(myCar);
}

public String toString(){
return "This is a " + color + " car!";
}
}

2. Calling Methods

After declaring different methods, we then can call the method on the object inside the main() method. When a method is called, the compiler executes every statement contained within the main() method.

// main methodpublic static void main(String [] args) {   Person.get_value();  }}

In Java programming language, we use the dot operator (.) to access the variables and methods of an object. In the example above, we are accessing the person class.

public class Person {   int age;   public static void main(String [] args) {     Person p = new Person();// here we use dot notation to set age   p.age = 20;// here we use dot notation to access age and print   System.out.println("Age is " + p.age);// Output: Age is 20   }}

3. Add to the method

There are two broad types of ways to add information to the method. Variables can be declared inside a method’s scope using { and }. Variable that declared inside the method cannot be used outside the scope of that method.

class Car {
String color;
int milesDriven;

public Car(String carColor) {
color = carColor;
milesDriven = 0;
}

public void drive() {
String message = "Miles driven: " + milesDriven;
System.out.println(message);
}

public static void main(String[] args){
Car myFastCar = new Car("red");
myFastCar.drive();
}
}

In the example above, the variable message which is declared inside of drive method cannot be used inside main() and it can only be used inside drive(). While variable color or milesDriven are declared at the top of the class which can be used inside all methods in the class.

public void startRadio(double stationNum, String stationName) {
}

In the example above, this StartRadio() accepts a double parameter and a String paramter stationName. When we call the method in main() and provide with parameters, this may affect the way the results are shown.

Resources:

If you find any of the above information useful, please give me a LIKE and SUBSCRIBE. Drop me a comment if you have any questions!

Don’t forget to give me your 👏 !

--

--

Do DON Done

This is Donna and I write about Tech, Measurement & Analytics