• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

OOP-4; Help - I can't access my Objects!

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
I hope this doesn't give too much detail away - please edit, if necessary!
I've set up the static HashMap and have put the keys and objects in e.g.
hashMap.put( serialNumber , new Video( data item1 , data item2 , data item3, data item4 ) ) ;
But Thing.getDescription( ) ; complains that (correctly!) I'm trying to access a non-static method from a static context (it's not happy when I put Video.getDescription( ) ; either!)
I'm using a constructor to set up the items - this works OK if I instatiate the objects, but then I still have to call the instatiate-name.getDescription( ) ; rather than Thing.getDescription( ) ;
Has anyone got any suggestion as to where I should start looking for a solution?
All the best,
Kate!!
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ummm, a bit confused, but that's normal since not giving away too much usually results in obscure posts
You should be getting objects from your hashmap, for one, to get the descriptions on, right? Since all of your objects should Things, you should be able to reference those objects that way. There shouldn't be any static calling going on, really.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've set up the static HashMap and have put the keys and objects in e.g.
hashMap.put( serialNumber , new Video( data item1 , data item2 , data item3, data item4 ) ) ;


Yep. That's good.

But Thing.getDescription( ) ; complains that (correctly!) I'm trying to access a non-static method from a static context

Which static context are you trying to reach Thing.getDescription() from? getDescription isn't a static method so using the className.method() statement won't work, if you're trying to do this literally.

(it's not happy when I put Video.getDescription( ) ; either!)

The method should not be in the HashMap.

I'm using a constructor to set up the items - this works OK if I instatiate the objects, but then I still have to call the instatiate-name.getDescription( ) ; rather than Thing.getDescription( ) ;

Yes, this is the way to do it.
 
Kate Head
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks for the encouragement - I spent most of my lunch-hour in the reference library reading up a Java2 reference book, and I think I might have a solution - just posted for Marilyn to nitpick!
How do people do this assignment in one go? Well done to them is all I can say!
All the best,
Kate!!
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How do people do this assignment in one go?


Easy. They learn less along the way. (With all due respect to those immortals who get it the first time, of course. )
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Methinks thou art jealous, Lady Pauline!
Honestly, by the time I got to OOP-4 I had done a LOT of object oriented playing around on my own. The very basic UML diagram provided by the assignment pretty much gave me my lead. But definitely the main benefit was sitting down and writing program after program playing around with all the possibilities of inheritance and polymorphism. One day it just clicked, and luckily it was before I got to this assignment
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jealous? Moi? As if I have time for that, I mean really! rofl
You *are* my favorite geek though. Of all time. Really. No I mean it. Honest.
 
Kate Head
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Calm down everyone - I managed to win through on the 5th go - and I've learnt a lot (the bits they don't teach you on courses, for example!)
And the author of "Gorky Park" is Martin Cruz Smith, just in case you're interested (I hate not knowing that sort of thing - and it could come up in a Trivial Pursuit game or at a quiz night).
Many thanks to everyone for all their suggestions - and, of course, to Marilyn for being so patient when I kept on repeating my errors.
All the best,
Kate!!
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic