aspose file tools*
The moose likes Beginning Java and the fly likes Array with objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array with objects" Watch "Array with objects" New topic
Author

Array with objects

Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
Hi all, first post here.

I've been having this problem and suddenly thought "Is this the equivalent of trying to fix my car trouble by experimenting with different types of gas, when the real problem is I've put the wheels on the roof...?". In other words, I'm wondering if my program design is just plain wrong. I've done some OOP in C++ before, but Java is not C++ I'm beginning to notice. So here's what I've done:

I made a pretty schoolbook type class by the name Terms, with three private data members 'term', 'category' and 'definition', all of data type String. I made three public get methods, each of which returns the value of one of the data mambers. I made a public set method that receives three strings as arguments and assigns these to the three data members. Finally, I've made a public constructor which calls the set method and passes three empty strings as arguments. So far, so good.

To test the use of this class, I've then made a simple console application where in PSVM (public static void main) I create single instances of the objects and use all the methods. That works fine.

The problem begins when I create an array of the data type Terms (the name of the class I created before). In PSVM, I try to run a small for-loop to fill the array with a few terms, and then another for-loop to display the contents just using the get methods of the class Terms. I get an error message of the type "non-static method cannot be called from static method main" (cannot remember the exact wording - I'm sitting at work now and don't have the message in front of me, sorry). Okay, so I try to move these operations to separate methods called fillArray() and displayArray(), but these methods cannot be called from static method main either.

So, is my design fundamentally flawed? I am trying to force my car to drive upside-down so to speak? Although it might be possible to make that run somehow, I'd rather learn proper design, the way Java is intended to work...
(If it doesn't sound like an obvious problem with the design, I guess it's a syntax error in the code, and I'll have to supply it when I get home later today.)

Grateful for all suggestions!
Flo
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30929
    
158

Flo,
The design is fine. Your main method needs to instantiate an object of the type of class (I think it was Terms) and call the methods on that object.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
Oh, sorry didn't mention that. I tried to do something along the lines of

for ( int i = 0; i < 5; i++ )
{
// lines of code to input three strings to local String variables

// create new object of type Terms and then pass data to it
Terms tempTerm = new Terms();
tempTerm.setData( tempStr1, tempStr2, tempStr3 );
// assign object to array a
a[ i ] = tempTerm;
}

And this was not allowed - I got the impression from the errors that I couldn't assign data to the array from a static method.

Thanks!
Flo
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Where is "a" declared (inside or outside of main?) If it's outside, it may be a member variable, so the static main() routine couldn't access it.

Maybe you should show us the whole class.


[Jess in Action][AskingGoodQuestions]
Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
I have it at home, so won't be able to supply it for another seven hours or so (I'm in south-east Asia).

However, you may have located the problem there, Ernest. I declared the array before the start of main. If I declare it inside main and then want to have other methods perform operations on the array, can I pass it as a parameter? I can't remember this - arrays are objects so they would be passed ... call-by-reference? Right? So I should be able to declare it inside main, and if necessary, have a method that sorts the whole array and then returns it to main in sorted order, or a method the searches for a specific item.

Hmmm, will try this, and if I'm still stuck, I'll provide the code.
Thanks Ernest! Thanks Jeanne!
Flo
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If you declare it inside main(), you can pass it to the other routines as a parameter. If you declare it outside of main, and want all the static methods to be able to access it, then you just need to declare it to be static too:

private static Terms[] a = new Terms[5];
Catalin Merfu
Ranch Hand

Joined: May 26, 2004
Posts: 42
Is this the case:

1. The PSVM is a static method of your main class.
2. Your main class has a non-static (instance) method you are trying
to call from main().

See if you can turn the instance method static, that would resolve the problem.


Catalin Merfu<br /><a href="http://www.accendia.com" target="_blank" rel="nofollow">High Performance Java Networking</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array with objects