aspose file tools*
The moose likes Cattle Drive and the fly likes Seeking clarification on modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Seeking clarification on modifiers" Watch "Seeking clarification on modifiers" New topic
Author

Seeking clarification on modifiers

Betty Reynolds
Ranch Hand

Joined: Feb 16, 2000
Posts: 111
I haven't been able to dig up the answer yet, so can someone say why the main method has to be declared static?
As a result of this, it seems that any methods I call from within main have to be declared static. Otherwise, I get the following error:
someClass.java:17: Can't make static reference to method void someMethod(int, java.lang.String[]) in class someClass.
someMethod(someInt, someString);
^
1 error
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The JVM invokes the main() method before any instances of your class have been created, therefore main() must be static. Usually the first (and perhaps only) thing a main method does is create an instance of the enclosing class, and use that instance to invoke one or more non-static methods:
<code><pre>
public static void main(String[] args) {
MyClass instance = new MyClass();
instance.someMethod();
instance.somOtherMethod();
}
</pre></code>


"I'm not back." - Bill Harding, Twister
Betty Reynolds
Ranch Hand

Joined: Feb 16, 2000
Posts: 111
Jim,
Thanks for the explanation. I missed something very fundamental. If my understanding is correct, I have to create an instance of the class before I can start invoking methods to use within that class. If I don't, then my methods have to be static, in which case then they are then class methods rather than instance methods. Class methods appear to work but they probably aren't appropriate for these assignments.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20572
    ∞

All of the assignments in the cattle drive cover basic language use. I had some more assignments that went into inheritance and what not, but they were not as refined.
And with the number of students I have now, I'm glad I don't have all of the 30 or so assignments up there!
To answer your question... You should be able to do everything you need to do with static methods. In other words, you don't need to create an instance of your class.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Betty Reynolds
Ranch Hand

Joined: Feb 16, 2000
Posts: 111
Paul,
Have you considered creating another track of assigments that focus on OO concepts?
My background is legacy systems development (i.e., assembler, COBOL, mainframe DB's, etc.) and I am trying to make the transition to emerging technologies.
For me, picking up the mechanics of this language is easy. What is difficult is making the transition to a new paradigm. I don't have the time, right now, to devote to reading books to gain an in-depth insight into object oriented concepts (I just know the basics), and a lot of the literature that is out there now is just confusing or over my head at the moment (i.e., ExtemeProgramming).
Jim states above that the main method is primarily used to create an instance of the class being implemented. This makes sense if I were designing and thinking in OO terms.
As it is now, I'm afraid that I will just be applying the java language in the context of traditional program design. For example, I use main primarily to perform initial housekeepping (i.e., validate the parameters passed from the command line).
In fact, I am beginning to structure my applications along traditional programming lines (i.e., main invokes a method to do initial housekeeping, invokes a method to do most of the work, and then terminates).
This was probably not the intent of the developers of this language, but in absence of tasks that encourage using an object oriented approach, this is the most natural approach for me.
Any suggestions on how to get training (versus knowledge) in OO analysis and design?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Note that I'm ignorant of what's in Paul's assignments - I was addressing typical uses of main that I've seen. Usually the problem is that someone already has a class with instance methods that they want to access, and they don't understand why they can't do so directly from main() - that's the situation I was addressing. But there's no rule that says any objects must be instantiated, if you can do what you need to with static methods. It probably would be good to develop your feel for OO methodology, but I don't want to suggest that it's the only way to go.
Ray Marsh
Ranch Hand

Joined: Jan 12, 2000
Posts: 458
Betty,
I'm in the same boat as you are right now. The IBM mid-range AS400 system, RPG and DB2 are my primary skill sets.
I am going through the same issues of trying to grasp the conceptual part of OO. The mechanics are easy for an experienced programmer, but a whole new approach is another story.
Here's what has worked and is working for me.
1. Don't rush, take your time. It takes a while for it to sink in. Just recently light bulbs are starting to come on and some previously puzzling concepts are making sense.
2. Find time to read a little at least. The book reviews on this site are helpful. Paul Wheaton aka "Trail-boss" recommends "Just Java 2" I just bought it and it is very good, so far. The explanations are basic and clear.
3. Start writing code. This has helped a lot. Join the cattle drive, if you haven't already.
4. Keep at it. At times it has been very frustrating and discouraging but the gang here at the ranch are good folks and extremely helpful. Watch out for the trail-boss he's a tough one! Pretty smart for an old cow-poke too.
Hope this helps.
Ray


Anxiety does not empty tomorrow of its sorrows, but only empties today of its strength. – Charles Spurgeon
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
It looks that there are many of us ex- or present structured programmers making the transition to modern methodologies
For me too, Java is the first real OOP experience. It has been really interesting, learning a new paradigm and everything. I "thank god" that I could go from Structured Programming in C to OOP directly in Java without the torture of C++ (well, that's not my opinion, I understand that the "Fathers of Java" also hated C++).
And I didn't lift that quote from PvdL's Just Java 2; he tells of a friend of his that told him the exact same thing!
Finally, make sure that you get the errata at http://www.afu.com/jj4.txt.

[This message has been edited by Tony Alicea (edited February 20, 2000).]


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20572
    ∞

Betty,
I already have several assignments on OO (much like the existing cattle drive), but I'm currently hiding them. I currently have about six students doing the regular cattle drive and based on your own experience, I'm sure you can guess how much time it takes.
What you see now is "chapter 1" - hence that is why all of the assignments start with "1.". I have about 20 chapters.
Maybe what I should do is say that people should not e-mail me their later chapter assignments, but instead have the answers posted and it will be a do-it-yourself sort of thing. Questions could be posted here.
Betty Reynolds
Ranch Hand

Joined: Feb 16, 2000
Posts: 111
Paul
That would be great!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Seeking clarification on modifiers