• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

DateFormat.getDateInstance(int style)  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
please explain how does DateFormat.getDateInstance(int style) work?
DateFormat is an abstract class,it can't be instantiated then what instance does getDateInstance(int style) return?
(Can an Abstract class have instances?)
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sonali raina wrote:Hi
please explain how does DateFormat.getDateInstance(int style) work?
DateFormat is an abstract class,it can't be instantiated then what instance does getDateInstance(int style) return?
(Can an Abstract class have instances?)


Hi sonali,

Welcome to CodeRanch!

If you see the method invocation carefully, you are not at all creating an instance of DateFormat.

In fact, getDateInstance(int style) is a static method - so instance(or object) is not necessary to create an object.

I hope this helps.
 
Ranch Hand
Posts: 257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I will try to answer this. Hope its not an overkill. Lets look at the source code for DateFormat class here
Look at the package of that class. We are interested in "java.text.DateFormat"

Go to your java installation folder. It looks like jdk<some number here>. Find the file src.zip. Extract its contents. Follow this path - java.text.DateFormat. Open the file. Find getDateInstance() or getDateInstance(int abc).



Its calling get(XXX)...search for that method.



Abstract class cannot be instantiated. So, it does NOT return an Object of DateFormat which is an abstract class. But, it returns and Object of SimpleDateFormat. Lets see what SimpleDateFormat is all about.
We look at SimpleDateFormat here

public class SimpleDateFormat
extends DateFormat


It IS A type of DateFormat. Its a class which can be instantiated.

Hope it makes sense.


 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also remembered that you can use ObjectABC.getClass() to get the class name. Try it and tell us what you get. Docs here
 
sonali raina
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anayonkar shivalkar for the help.
 
sonali raina
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Andy Jack.The explanation is really helpful.
 
Forget Steve. Look at this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!