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);
		

	}

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.

> 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..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.