Menu
Topics Index
...
`

Java Method | Java Constructor

A method in object-oriented programming (OOP) is a procedure associated with an object. An object can contains data and behavior, which form the interface that an object presents to the outside world. Data is represented as properties of the object and behavior as methods. A Java method is defined as a collection of statements which are grouped together for performing a certain operation.

For example, if you call the Java method System.out.println() then internally it executes multiple statements for to displaying a message on the console. Each Java method has its own name. When the method name exists in any part of the program code then the control goes to the body of that method and executes that Java method. If the method execution is completed then the control comes back to the next line of the program code from where it calls the method.

Java method is a time saver, because they are useful to not writing the repetition of same code again and again. i.e., We just simply call the method name where ever that block of same code needed. Java methods are classified into two types.

(i) Built-in Methods - Compiler package can handle the built-in methods such as System.exit(0) & System.out.println().
(ii) User-defined Methods - This type of Java method can be created by the user or programmer. Users can give names for this methods and also define tasks to perform in those methods.
The syntax of Java method can be as follows:


  • modifier returnType nameOfMethod (Parameter List)
    {
         // method body
    }

A method should be called for using it. Java method can be called in two ways. i.e., by returns a value and not returns any value. By calling method name, a method can be executed.

When an object is created, a Java constructor initializes it. As its class, Java constructor contains the same name and is similar to a method in syntactical way. Anyway, a Java constructor doesn't have any explicit return type. Typically, a Java constructor can be used for giving initial values to the instance variables which are defined by the class; or to performing any other startup procedures which are required for creating a fully formed object.

Whether you define a constructor or not, every class can have constructors; because a default Java constructor is automatically provided by Java that initializes every member variable to zero. Anyway, once our own Java constructor is defined, there is no longer to use the default Java constructor.

More often, we needed a Java constructor which accepts one or more parameters. A Java constructor can be added by the parameters in the same manner they added to a method. After the constructors name, just declare them inside the parenthesis.

This chapter deeply covers the topics Java Methods, Basic Java methods, parameter passing & scope, recursive methods,etc with examples which explained below.

In this section, we'll go into detail about:
6.1 Basic Java Methods
  • Java supports creation of methods which help mainly in removing duplicate code, reducing code complexity and increasing its maintainability. For example, the following ...Read More

6.2 Java Methods
  • Every Java method declared contains a method name, input parameters and return type. These 3 things comprise the method signature. The definition includes the body along with...Read More

6.3 Java Methods - Parameter Passing And Scope
  • It is very important to understand how the parameters are passed from the calling method to the called method and how the return value is sent back to the calling method. As discussed...Read More

6.4 Java Program To Find Simple Interest Using Methods
  • This program explains how to calculate simple interest using methods. The formula for calculating simple interest is I = P * T * R / 100.0 where I is interest, P is principal, T is...Read More

6.5 Recursive In Java
  • A method which calls itself is called recursive method. Recursive methods are very useful when we want to process data stored in list structure or in tree structure. Some types ...Read More

Dependent Topics : 5. Control Statements 

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App