• Post Reply Bookmark Topic Watch Topic
  • New Topic

array of objects  RSS feed

 
Rajesh Krmar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a object of a class and stored it in a arry of Objects.
when i am trying to access any method of that class, i am able to call it using reference of that calss, but not able with the arry element, where i have stored that ref in arry.
ie.
Trial t = new Trial();
t.example(); It works.

But when,
Object objArr[] = new Object[2];
objArr[0]=t;
And when i say,
objArr[0].
It do shows the methods of the class.

why?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you have defined an array of Objects. The compiler only knows that there are Objects in there, not that these objects are Trials, so you only have the methods available to an Object. Either declare the array as an array of Trials:

or cast the object to Trials:
 
Rajesh Krmar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!