• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is data type of Object reference as Class?  RSS feed

 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't understand why is the data type of an Object reference taken as its class type?
Afaik ,
Class_name Obj_name = new Class_name( ) ;

Here, Obj_name holds the reference to the object of Class , Class_name, i.e; just a number ( address )
so, why is there a need of making the type of Obj_name as its class type ??
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because Java is a Statically Typed language. Which essentially means that you have to provide the type of a variable up front for the compiler, leading to declarations such as:

For other languages that do dynamic typing, such as Groovy, the type is inferred at runtime by the compiler. In Groovy you could do:

Which I think was the crux of your question, why do we need to give a type to the variable object?

Some further reading: Wikipedia: Type Inference
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeap.
Got cleard.
Neways, Thanks.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also checkout dynamic binding concept where the invocation of method referred to runtime behaviour.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!