Introducing Java Parameters, Arguments And Constructors
Parameters And Arguments
Information can be passed to methods as parameter. Parameters act as variables inside the method.
Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
int b = 7;
Integer Variable Value
string str = "Java" ; Object?
public static voiid main(string[]args){
}
void my height(){
}
return type
methodname
void add Numbers(){
int a = 6;
int b = 4;
int c = a = b;
System.out.print(c);
}
return type - 9
The parameter is there to keep Input and Output. And the Return Type is there to return the Output.
Constructor
A Constructor is a Method. But this is a special method.
1. The constructor must be created with the same name as the class.
2. There is no return type.
Ex:-
class Java {
Java () { - Constructor
}
}
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.
Constructor cannot be run in all cases. The constructor can be called only when the object is created. That is, a constructor is a "Method that is called only when creating an Object that does not have a Return Type created by the same name as the Class."
Constructor Calling
1. When an application is run, the constructor is the first to be executed
2. When the program starts to run, the work to be done is coded in the constructor.
3. A Constructor that can pass Values and use parameters and arguments, even if it doesn't have a Return Type.
Notes: In order to run a Java program, the following points must be present.
1. class
2. constructor
3. main method
4. object
Comments
Post a Comment