Java Method | Java ConstructorA 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.
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
6.2 Java Methods
6.3 Java Methods - Parameter Passing And Scope
6.4 Java Program To Find Simple Interest Using Methods
6.5 Recursive In Java
Dependent Topics : 5. Control Statements |