0

Hi,I am new in Java ,and I was wondering if somebody can explain what is the right way to create new object.Is the only right way the case 3,Am I only overwriting the arguments in the same object in case 1 and 2
Thanks

public static void main(String[] args) {
		Date dateObj;
		System.out.println(new Date(12,12,2000)); //case1
		System.out.println(new Date(12,12,2000));//case2
		Date dateObj2=new Date(11,11,2111);      //case 3
		System.out.println(dateObj2);
		

	}
4
Contributors
3
Replies
6
Views
6 Years
Discussion Span
Last Post by ~s.o.s~
0

Each of the 3 cases creates a new Date object. After executing your code you will have 3 Date objects. Because you did not keep any reference to the first two you will not be able to access them again, and they will be garbage collected. The third Date object can be accessed through the variable dateObj2.

0

> what is the right way to create new object

All the Date class constructors except the no-arg and the one which takes milliseconds are deprecated. If the purpose of your question was to find the right way to create a Date object, look into the Calendar and SimpleDateFormat classes as already suggested..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.