We will learn constructor overload in java.
public class ConstOverLoad {
public static void main(String[] args) {
Hello hello;
hello = new Hello(5);
hello.greet();
}
}
class Hello {
int count;
//Constructor Overloading
Hello() {
this.count = 1;
}
//Constructor
//Must sameWith className
//No return type Eg: void
//Constructor type can't be same with other Const
Hello(int count) {
if(count > 0) {
this.count = count;
}
}
void greet() {
for( int i = 0; i < 5; i++) {
System.out.println("Hello Java Method");
}
}
//Init block can initialize
//14 Writing Constructor.mp4
}
Code Block in Java.
import java.util.Properties;
import java.io.FileInputStream;
public class CodeBlock {
public static void main(String[] args) {
Hello hello = new Hello();
hello.show();
}
}
class Hello {
//Initializing
//Under class, only write declaration
//Instance Variable
String name;
int count;
//CodeBlock for initialization instance variable
{
try(FileInputStream input = new FileInputStream("settings.properties")) {
Properties prop = new Properties();
prop.load(input);
name = prop.getProperty("name");
count = Integer.valueOf(prop.getProperty("count"));
} catch(Exception e) {
e.printStackTrace();
}
}
void show() {
for (int i = 0; i < count; i++) {
System.out.println(name);
}
}
}
Keep Calm and Code On
Thanks for your time.