Parameters, Arguments & Constructors

Parameters, Arguments & Constructors

Generally Java program is executed line by line. Therefore, in order to run any code, if there are other codes required for that, they should be mentioned beforehand. Generally Java program is executed line by line. Therefore, in order to run any code, if there are other codes required for that, they should be mentioned beforehand. Also, there are 4 essential elements to run a Java code. They are, 

  1. Class
  2. Constructor
  3. Main method
  4. Object
Figure 1: Simple java code

Parameters & Arguments

A method requires 2 main elements. They are, return type and method name with a parameter list. The return type is used to return the output values and has 2 types. Void is Java's default return type. It can be used with any data type. String or any other data type can also be used as a return type with the "return" keyword. The "return" keyword is used to indicate that the return type of the code is a type. It can only return values of the corresponding data type.


Figure 2: Return types

The parentheses inside the method are called as parameter list and the parentheses where the method is called are called as argument list. There are variables in the parameter list and the values related to the variables in the parameter list are shown in the argument list.

Figure 3: Parameters & Argument list

Constructors

The method which is created by the name of the class, has no return type and can be called only when the object is created is called constructor. Although there is no return type, the constructor can use parameters, arguments by passing values. When the application is run, the first thing that works is the constructor, so when the program is run, the first things that need to happen are coded inside the constructor. 


Figure 4: Constructor

Comments

Popular posts from this blog

Polymorphism

Java Standards, Class & Object

Super Keyword