+ 2

Why i can make for some classes a new class, for some not

for example: 1) DateFormat df= DateFormat.getDateInstance(DateFormat.Full); 2) SimpleDateFormat newdf= new SimpleFormat("bla"); by first one if I use new after equal symbol, I'll get error by second one there is already a new, and it works. I am literly new with Java :)

30th Apr 2018, 7:49 PM
Sam
Sam - avatar
1 Odpowiedź
+ 8
On the first code example you are calling the static method getDateInstance of the DateFormat class. You don't need to create an object using the new operator then because the method handles the initialization for you and returns the object. Static methods belong to the class and not to an object of the class, so you don't need an object to call the method. You can also have static fields of a class, for example to count how often the class is initialized.
30th Apr 2018, 8:38 PM
Tashi N
Tashi N - avatar