• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array with objects

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Flo Powers
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Flo Powers
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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];
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic