Hello,This will definitively useful for beginners.At the startating stage of developing developer should know some basic things. Like how to maintain global variable and function and use them in all necessary class to optimize code.I will show you how to maintain this kind of thing.
For that you have to create Singleton Class and learn how to use it.But before that we you should know that what is Singleton Class ?
What is Singleton Class ?
Only one instance of the class can exist. As per Google
How ClassicSingleton looks ?
Let start with creating class said
How to use function and member of the Singleton class ?
For that you have to create Singleton Class and learn how to use it.But before that we you should know that what is Singleton Class ?
What is Singleton Class ?
Only one instance of the class can exist. As per Google
How ClassicSingleton looks ?
Let start with creating class said
ClassicSingleton.java
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
| public class ClassicSingleton { private static ClassicSingleton instance = null ; public char [] name = new char [ 18 ]; // Member protected ClassicSingleton() { // Exists only to defeat instantiation. } public static ClassicSingleton getInstance() { if (instance == null ) { instance = new ClassicSingleton(); } return instance; } public String getName() { String myName = "Chintan Khetiya" ; return myName; } } |
public class ClassicSingleton { private static ClassicSingleton instance = null; public char[] name = new char[18]; // Member protected ClassicSingleton() { // Exists only to defeat instantiation. } public static ClassicSingleton getInstance() { if (instance == null) { instance = new ClassicSingleton(); } return instance; } public String getName() { String myName = "Chintan Khetiya"; return myName; } }
How to use function and member of the Singleton class ?
1
2
3
4
5
| ClassicSingleton CS; CS.getInstance(); //... String myName = CS.getname(); // use function |
ClassicSingleton CS; CS.getInstance(); //... String myName = CS.getname(); // use function
No comments:
Post a Comment