aspose file tools*
The moose likes Beginning Java and the fly likes static & non-static contexts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static & non-static contexts" Watch "static & non-static contexts" New topic
Author

static & non-static contexts

edgar mcdermott
Greenhorn

Joined: Aug 29, 2002
Posts: 4
Hi,
I have a question that's probably very simple but which is frustrating me to no end. Okay - I've got two objects, Job and JobCollection. Neither one has a "main" method. Job has 2 methods, setJobID and setJobName. JobCollection has 1 method, addJob. They're all 'public' w/ a 'void' return type. I made a separate class (with a "main" method) which prompts me for a Job ID & a Job Name, then calls the methods of "Job" to set those variables. No problems there. But if I try to call "addJob" from "JobCollection", I get the error message "can't call non-static method addJob from a static context" (when compiling).
So here's the question. I can understand the error- nothing in JobCollection is declared as static, and I'm trying to call it from within "main". But nothing in Job is static, either, and I don't get any compile errors from that object. It makes no sense to me. If anyone has any suggestions, I'd be very grateful. Please forgive me if I am not explaining this very well - I am a true newbie!
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Here's some sample code:

Can you show how you are doing it?
edgar mcdermott
Greenhorn

Joined: Aug 29, 2002
Posts: 4
Hi,
Thanks for the reply. Your code is very similar to what I've got. In fact, leaving out the console input stuff, it's just about exactly the same. The only difference is in the JobCollection class, which looks like this (I tried to use the Collection interface) -

I had thought that maybe the ArrayList type had something to do with it, but if I comment out all of the references to that and just make addJob an empty method, I still get the error. Weird! I didn't try the "print" test ... I'll have a go at that, see if I get some clues.
Thanks!
Greg Ostravich
Ranch Hand

Joined: Jul 11, 2002
Posts: 112
Can I ask why you create a JobCollection class?
Are you extending an existing collection or creating functionality that an existing collection wouldn't work for?
OK - you posted the same time I did so I see what you did now , but my question still applies.
Couldn't you do this?

FYI: Another option to print (depending on the circumstance) is to include a toString() method which overrides the default. In Anthony's example below mine if you had a toString you could put your object in a print or other context where a String is used and it will do it automatically. That wouldn't handle what to do for an individual entry, but you could do the same thing with your Job object and if you get an individual instance you could print that instance.
e.g.

[ August 29, 2002: Message edited by: Greg Ostravich ]
[ August 29, 2002: Message edited by: Greg Ostravich ]

Greg Ostravich - SCPJ2
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Okay, I made a JobCollection a collection wrapper class:
edgar mcdermott
Greenhorn

Joined: Aug 29, 2002
Posts: 4
Uhhh, basically I guess I'm creating a JobCollection class because my boss told me that's the way to do it! The idea is to query a database and treat the rows returned as Job objects; if we've got multiple Jobs we'll put 'em in a collection so we can iterate through them and stuff like that. I'm still a bit fuzzy on the whole Collection thing, obviously.
By the way, I tried Anthony's code exactly as is and it's working fine. So obviously there's some difference which is tripping me up, and I'll use this as a starting point to find it. I appreciate the help.
edgar mcdermott
Greenhorn

Joined: Aug 29, 2002
Posts: 4
Hi all,
I am an idiot! Found the problem. When calling addJob from the "front-end" class, I did it like this:

instead of this:

So instead of using the specific instance of that JobCollection, I was referring to the general type. How stupid. It all makes sense now. The error I was getting led me to believe it was more of a structural design problem than a simple typo.
Thanks!!
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Glad to help
 
jQuery in Action, 2nd edition
 
subject: static & non-static contexts