• Post Reply Bookmark Topic Watch Topic
  • New Topic

When and how would I use Object obj = new someClass()  RSS feed

 
Nigel Summers
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
being new to Java, occasionally I come accross a line of code that I really don't know how it is used, when it is used or even why it is used, in this case the line of code is Object obj = new someClass().

I understand the "normal" instantiation of someClass aName = new someClass(); but not using Object.

can anyone please explain how, when and why I should use it
 
Paul Clapham
Sheriff
Posts: 22527
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should use it when you don't care that the type of the object is anything more specific than Object. (That should take care of "when" and "why"; you've already shown "how").

I suppose you might come back and say "But when wouldn't I care about the type of the object I create?" Well, I don't know. Not very often, I expect. But if you're saying you saw this in real code (not in some exam question) then why don't you ask about the real code? The person who wrote it must have had a reason, so maybe looking for that reason would help find reasons for doing it.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sometimes, you don't know what you're going to get back. Granted, if you are doing a "...new SomeSpecificClass()", you will. but sometimes, you may be calling a method that could return one of several possible classes.

Hopefully you'd have SOME idea what your getting back - something like an interface, but if you don't, Object will always work.

I'm not saying the above is a GOOD reason, fwiw.
 
Paul Clapham
Sheriff
Posts: 22527
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I'm not saying the above is a GOOD reason, fwiw.


That's a good point... everything in a program is there for a reason, but assuming that reason is a good reason is not always correct. You could look at The Daily WTF for many examples of that, but I warn you, you'll spend a large amount of your day there before you can tear yourself away.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One common reason to write a statement like

where "Something" can even be the name of an interface instead of a class, is in using collection classes. For example like this:

You do it this way because the rest of the program only needs to know that "values" is a List; it doesn't need to know what specific implementation of interface List (in this case, ArrayList) is used. If ater you discover that another implementation of List (for example LinkedList) would be a better choice, then you'd only have to change that one line of code - the rest of the program doesn't need to be modified because "values" will still be a List.

This concept is more general than collection classes, but collection classes is a common use case for this concept. It's the program to an interface, not an implementation principle.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!