Skip to content

Commit f0bf191

Browse files
author
bhuwang
committed
Added different object creating strategy
1 parent 539152a commit f0bf191

File tree

7 files changed

+129
-18
lines changed

7 files changed

+129
-18
lines changed

Test1.ser

66 Bytes
Binary file not shown.

src/com/bhuwan/java/basics/abstractclass/AbstractDemo.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class AbstractDemo {
1111
public static void main(String[] args) {
1212
// abstract class cannot be instantiated
13-
13+
// Animal animal = new Animal();
1414
}
1515

1616
}

src/com/bhuwan/java/basics/abstractclass/Animal.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
/**
77
* @author bhuwan
88
*/
9-
public abstract class Animal implements Species{
9+
public abstract class Animal implements Species {
1010
String name;
1111

1212
public abstract String getSound();
1313

1414
public String getName() {
1515
return name;
1616
}
17+
18+
public Animal() {
19+
}
1720
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
*
3+
*/
4+
package com.bhuwan.java.basics.oop.constructor;
5+
6+
/**
7+
* @author bhuwan
8+
*
9+
*/
10+
public class ConstructorDemo {
11+
12+
public ConstructorDemo() {
13+
System.out.println("First line should be super");
14+
// this(5);
15+
}
16+
17+
public ConstructorDemo(int a) {
18+
// First line should be super() or this()
19+
System.out.println("Hello");
20+
// this("Bhuwan");
21+
}
22+
23+
public ConstructorDemo(String a) {
24+
// if we have a recursive constructor call then we will have CTE
25+
this(10);
26+
}
27+
28+
public ConstructorDemo(double a) {
29+
super();
30+
// this(10);
31+
}
32+
33+
public static void main(String[] args) {
34+
35+
}
36+
}
37+
38+
abstract class AbstractClass {
39+
//
40+
}

src/com/bhuwan/java/basics/oop/createobject/CreateObjectDemo.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public static void main(String[] args)
5252
Runtime runtime = Runtime.getRuntime();
5353
DateFormat instance2 = DateFormat.getInstance();
5454
Calendar instance = Calendar.getInstance();
55+
Test1 test6 = Test1.getInstance();
56+
System.out.println(test6.m1("4. Using factory method."));
5557

5658
// 5. Using Clone method.
5759
// you must have to override the clone() method.
@@ -68,18 +70,16 @@ public static void main(String[] args)
6870
ObjectInputStream ois = new ObjectInputStream(fis);
6971
Test1 test5 = (Test1) ois.readObject();
7072
System.out.println(test5.m1("6. Using deserialization"));
71-
7273
}
7374

7475
}
7576

7677
class Test1 implements Cloneable, Serializable {
7778

78-
/**
79-
*
80-
*/
8179
private static final long serialVersionUID = 482351728504309257L;
8280

81+
private static Test1 currentInstance = new Test1();
82+
8383
public Test1() {
8484
}
8585

@@ -95,4 +95,9 @@ public String m1(String message) {
9595
protected Object clone() throws CloneNotSupportedException {
9696
return super.clone();
9797
}
98+
99+
public static Test1 getInstance() {
100+
return currentInstance;
101+
}
102+
98103
}

src/com/bhuwan/java/io/CreateNewFile.java

+16-12
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
import java.io.File;
77
import java.io.IOException;
8-
import java.nio.file.Files;
9-
import java.nio.file.Path;
10-
import java.nio.file.Paths;
118

129
/**
1310
* @author bhuwan
@@ -17,31 +14,38 @@ public class CreateNewFile {
1714
public static void main(String[] args) throws IOException {
1815
String fileSeparator = System.getProperty("file.separator");
1916
// absolute file name with path
20-
String absoluteFilePath = fileSeparator + "home" + fileSeparator + "bhuwan" + fileSeparator + "filedemo"
21-
+ fileSeparator + "file.txt";
17+
String absoluteFilePath = fileSeparator + "tmp" + fileSeparator + "file.txt";
2218
File file = new File(absoluteFilePath);
2319
if (file.createNewFile()) {
2420
System.out.println(absoluteFilePath + " File Created");
25-
}
26-
else
21+
} else {
2722
System.out.println("File " + absoluteFilePath + " already exists");
23+
}
2824

2925
// file name only
3026
file = new File("file.txt");
3127
if (file.createNewFile()) {
3228
System.out.println("file.txt File Created in Project root directory");
33-
}
34-
else
29+
} else {
3530
System.out.println("File file.txt already exists in project root directory");
31+
}
3632

3733
// relative path
38-
String relativePath = "tmp" + fileSeparator + "file.txt";
34+
String relativePath = fileSeparator + "tmp" + fileSeparator + "file.txt";
3935
file = new File(relativePath);
4036
if (file.createNewFile()) {
4137
System.out.println(relativePath + " File Created in Project root tmp directory");
42-
}
43-
else
38+
} else {
4439
System.out.println("File " + relativePath + " already exists in project root tmp directory");
40+
}
41+
42+
// create directory
43+
file = new File(fileSeparator + "tmp", "test_directory");
44+
if (file.mkdir()) {
45+
System.out.println("Directory named " + file + " created successfully.");
46+
} else {
47+
System.out.println("Directory already exist: " + file);
48+
}
4549
}
4650

4751
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
*
3+
*/
4+
package com.bhuwan.java.io;
5+
6+
import java.io.File;
7+
8+
/**
9+
* @author bhuwan
10+
*
11+
*/
12+
public class ListFilesInsideHomeDirectory {
13+
14+
/**
15+
* @param args
16+
*/
17+
public static void main(String[] args) {
18+
String fileSeparator = System.getProperty("file.separator");
19+
File homeDirectory = new File(fileSeparator + "home" + fileSeparator + "bhuwan");
20+
System.out.println(homeDirectory.list());
21+
String[] list = homeDirectory.list();
22+
for (String s : list) {
23+
System.out.println(s);
24+
}
25+
System.out.println("Total Files: " + list.length);
26+
27+
// display only directories
28+
int countDir = 0;
29+
for (String s : list) {
30+
File f1 = new File(homeDirectory, s);
31+
if (f1.isDirectory()) {
32+
countDir++;
33+
System.out.println(s);
34+
}
35+
}
36+
System.out.println("Total Directories: " + countDir);
37+
38+
// display only Files
39+
int countFiles = 0;
40+
for (String s : list) {
41+
File f1 = new File(homeDirectory, s);
42+
if (f1.isFile()) {
43+
countFiles++;
44+
System.out.println(s);
45+
}
46+
}
47+
System.out.println("Total Files: " + countFiles);
48+
49+
System.out.print("Sum of files + Dirs = total files: ");
50+
System.out.println((countFiles + countDir) == list.length);
51+
52+
// display root Files
53+
File[] listRoots = File.listRoots();
54+
for (File root : listRoots) {
55+
System.out.println("Root: " + root);
56+
}
57+
58+
}
59+
}

0 commit comments

Comments
 (0)