+ 1

How to create object in static members ?? Can anyone plz explain with example ???

26th Aug 2016, 4:57 PM
suji prem
suji prem - avatar
2 Antworten
+ 1
you can't create object s to static members. all static members will be initialized at compile time itself. you have to access static members with class name
26th Aug 2016, 5:47 PM
Gouda Kiran
Gouda Kiran - avatar
0
Yes you can. One of examples is a singleton pattern design. This pattern returns only one instance of the class. This could be archived by making class constructor private and creating a static method that returns only one instance. Ex: class Single{ private int age; private static Single onlyOne; private Single() {} public static Single get(int age){ if(onlyOne == null ){ onlyOne = new Single(); } onlyOne.age = age; return onlyOne; } public void show(){ if(onlyOne != null) { System.out.println(age) ; } } } public class Program { public static void main(String[] args) { Single s = Single.get(26); s.show(); } }
26th Aug 2016, 6:48 PM
Tiger
Tiger - avatar