Sitemap
Dhushanthini Archunan

Dhushanthini Archunan

Software Engineer

Java methods explained

4 min readJul 19, 2022

--

Press enter or click to view image in full size

A group of logical Java statements created to carry out a certain task can be referred to as a java method. They offer a mechanism to reuse code without having to write it from scratch again. In contrast to Python, C, and C++, every method in Java should belong to a different class. Without a Java class, methods are not feasible to exist.

The following is a list of the elements needed to create Java methods:

Access Modifier

In java, there exist four different types of access modifiers:

  • Public: All classes in an application have access to methods that have been marked as public.
  • Protected: Methods declared as protected are accessible from the class within which it is defined and all subclasses of that class.
  • Private: Methods declared as private are only accessible from the class within which it is defined.
  • Default: Methods declared as default are accessible from the class within which it is defined and from classes declared within the same package as the class enclosing the method.

Return Type

  • This contains the data type of the value the method is supposed to return, or it is void if the method does not return anything.

Method Name

  • This is the name assigned to the method, which may or may not be unique. It is to be noted that the method name should be verbs, and words used show follow camel case notation.

Parameters

  • This includes a list of input parameters with their associated data types, separated by commas. If the method has no input arguments, the operator () is utilized.

Exceptions

  • If a method is capable of throwing one or more exceptions, we can put them in a list with commas separating them.

Method Body

  • It is the code that is enclosed in brackets. One or more logical statements for carrying out a certain task are contained in the method body.

You probably have a method, process, or strategy for writing code if you’re a programmer. In order to construct a Java class, we can follow these steps:

  • Figure out what the class is supposed to do.
  • List the instance variables and methods.
  • Write prep code for the methods.
  • Write test code for the methods.
  • Implement the class.
  • Test the methods.
  • Debug and re-implement as needed.

Three things when you write code:

Get Dhushanthini Archunan’s stories in your inbox

Join Medium for free to get updates from this writer.

1. Prepcode — A form of pseudocode, to help you focus on the logic without stressing about syntax.

2. Test code — A class or method that will test the real code and validate that it’s doing the right thing.

3. Real (Java) code — The actual implementation of the class. This is where we write real Java code.

Loop in programming

A loop is a control flow statement that repeatedly iterates a program element. In Java, loops are frequently utilized while creating programs. If the number of iterations is fixed, loops are advised. In Java, we use:

  • for loop
  • while loop
  • do…while loop

Regular (non-enhanced) for loops

1. Initialization: It is used to declare and initialize a variable to use within the loop body. You’ll most often use this variable as a counter.

2. Boolean test: The body of the loop is executed if the condition is true. The for loop’s body will not be executed if it is false; instead, control will go on to the next line after the for loop.

3. Iteration expression: The control returns to the iteration expression when the for loop’s body has been run. You can update any loop control variables using this statement. This statement can be left blank with a semicolon at the end.

While loop

While loop has only the Boolean tester and it doesn’t contain built-in initialization or iteration expression. While loop is used, when you don’t know the exact no of iterations(no of times to loop) and need to keep running until some condition is true.

The enhanced for loops

Press enter or click to view image in full size

The Java programming language provides a second type of for loop known as the enhanced for loop, which makes it easier to iterate through all the elements in an array of other types of collections.

Casting primitives in Java

Casting is the conversion of data of one type to another type either implicitly or explicitly. Casting happens for both primitive types and reference types. If the casting operation is safe, java will do automatic type casting. This is called implicit type casting. If java can’t be sure whether the casting will be safe, java will not do automatic casting, but the programmer can do the casting if he is sure about the outcome. This is called explicit type casting.

--

--