Note that if you want to use another method in a method parameter, you need to use a lambda or method reference to pass in this another method. Lambda and method reference will be discuss in the succeeding chapters.
Constructors - this is like a method but has the same name as the class name. Objects are called through the constructors. Of course, constructors can be overloaded too but make sure that the parameter varies so that the compiler can recognize it. If you happen to forget to create a constructor, the compiler will create a no-argument constructor for you which will be base on the super class of your class. If your class has no super class, the compiler will automatically use Object as its super class. Note that the automatic creation of no-argument class is available only for those class with no constructors which means that if you happen to create a constructor with argument, do not expect the compiler to create a no argument constructor for you. You have to create it yourself.
Example of a method using an array as a parameter
============================
public String studentName(String[] name){
//statements;
}
===========================
varargs - variable arguments. You can use this when you have no idea on how many arguments will be inputted in the parameters of the method or constructor. It is like saying the arguments in the parameter varies accordingly. You can treat varargs as an array. When declaring a varargs, you just do it like an array except that instead of putting brackets, you put ellipsis or three dots after the data type. Then, when you invoke it, it is safe to assume it like an array. You can check the example below most especially when invoked the method setName().
==========================
class Student{
public String setName(String... name){
String yourName = name[0];
for(int i=0; i<name.length; i++){
yourName += " ";
yourName += name[i];
}
return yourName;
}
public static void main(String[] args){
Student s = new Student();
s.setName("Johnny", "Bean", "English", "Jr.");
}
}
============================
Result:
Johnny Bean English Jr.
this - this keyword is used only when the parameter in the constructor or method has the same name as the field or properties of the class. When you use the this keyword, it would always refer to the field while that variable with the same name would always refer to the parameter. The compiler would always prioritize the local or parameter before going to the member fields.
=============================
class Student{
private String name;
public Student(String name){
this.name = name;
}
}
=========================
You can read it like this. This variable property named "name" has a value of a parameter variable named "name".
passing reference data type arguments - the object that you created from a class can be used as a reference to that class when it used as an argument to the parameter of a method. The pass in reference object can be pass into the method by value. So, when the method returns, the passed in reference still refers to the same object. The object's field can be changed in the method. Check the example below.
=========================
class YourHeightWeight{
private double height;
private double weight;
public YourHeightWeight(double h, double w){
height = h;
weight = w;
}
public void displayHeightWeight(){
System.out.println("height: " + height);
System.out.println("weight: " + weight);
}
public void changeHeightWeight(double h, double w){
height += h;
weight += w;
}
}
class PrintingInfo{
public static void main(String[] args){
YourHeightWeight hw = new YourHeightWeight(170.0, 155.0);
hw.displayHeightWeight();
hw.changeHeightWeight(2.5, 1.3);
hw.displayHeightWeight();
}
}
======================
Result:
height:170.0
weight: 155.0
height: 172.5
weight: 156.3
YOU ARE READING
Java Programming
Randomhttps://docs.oracle.com/javase/tutorial/ This is the link where I learned my java lessons online. I choose java as first programming language. I am a career shifter. I just want to start something new. I choose to write it here in Wattpad so that I...
Class Declaration
Start from the beginning
