• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Seeking clarification on modifiers

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Betty Reynolds
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Trailboss
Posts: 24069
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Betty Reynolds
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
paul wheaton
Trailboss
Posts: 24069
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul
That would be great!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic