English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

difference between
Calendar calendarObject=new GregorianCalendar() ;
calendarObject.get(Calendar.MONTH)
calendarObject.get(calendarObject.MONTH)
calendarObject.MONTH
i know the answers but what is actually the difference between the above three

2006-10-02 23:13:43 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

Congratulations for writing some code that describes the situation. You have the new statement to show the calendarObject is a Calendar, nice.

The difference between the two calls is:
- As the field Calender.MONTH is a static int, the actual memory of this field does not change. It will always be the same one. This is why, the second calendarObject.MONTH also works. You are best off using the static field to call a function, so that it can be visually seen that the field is a static one. If using the second way, you are not saying specifically that the field is static. This will help you when coding, and also others if they need to read your code, whether that be now or 20, 50 years down the track.

2006-10-06 00:17:19 · answer #1 · answered by Mark aka jack573 7 · 1 0

Calendar.MONTH: is a static final (i.e. constant) int value that represents the "month field".

calendarObject.MONTH: the compiler will look for an instance variable called MONTH for your object. When it fails to find it defined in GregorianCalendar, it will search in Calendar (direct superclass of GregorianCalendar) and discover the MONTH constant. The resulting byte code will have the actual literal int value stored in it.

Calling the get() method on the GregorianCalendar object called calendarObject will result in executing the Calendar get() method (inheritance/polymorphism in action). Since both Calendar.MONTH and calendarObject.MONTH resolve to the same literal value, which in turn refers to the month value stored by the calendarObject object, you get back the same value regardless of which form is used. Note that the value you get back from get() maps to the particular month that the calendar object represents. You used the default constructor when creating a GregorianCalendar, so the value will map to the month at the time of execution (e.g., right now it is October, so the value returned is 9, next month it will return 10, etc.). WARNING: according to the API documentation, not all implementations of Calendar subclasses are required to use the same literal value for MONTH.

Note to Dr. Mr. Ed: just mouse over the ellipses and your browser will show the rest of the text in a tooltip.

2006-10-03 16:17:08 · answer #2 · answered by vincentgl 5 · 0 0

Unfortunately, parts of your question was truncated -- you need to include some spaces to get all the text to appear (like adding them before the parens).

It's hard to answer without seeing the rest of the methods, but the final one access the public static constant MONTH for that object. The other one's look like they use different getter methods to accomplish the same task.

2006-10-03 06:48:10 · answer #3 · answered by Dr.Mr.Ed 5 · 0 0

fedest.com, questions and answers