• Post Reply Bookmark Topic Watch Topic
  • New Topic

Following a Java program  RSS feed

 
S Boz
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Java and am currently struggling to learn the basics. I understand the class system somewhat and feel quite confident on methods and basic loops, variables and other programming techniques.
The problem arises when I try and decipher some else’s Java program. I do not understand where a Java program starts or how to follow the programming thread so I can see how it functions. I do understand all Java programs use a 'Main' to start it but how do you find the 'Main' function and then where does it go from there?? Every time I try and do a Google search on ‘Deciphering a Java Program’ I get bogged down with Java tutorials. The Java tutorials do not explain how to take and existing Java program and show the ‘thread’ of its structure so someone can follow the logic. Can anyone point me to a good article that would explain how you go about deciphering a Java program?

Thanks
S. Boz
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Honestly, the best way to learn is 'practice, practice, practice'.

finding the main() method shouldn't be hard. It's in the .java file of whatever class you call with the "java <classname>" command. You could also grep through all the .java files and find it that way.

Once you find it, you have to follow the logic step by step - there is no shortcut. If you are simply reading the code, you may have to make some decisions, i.e. when you come across and 'if-else' statement, you may want to only follow one side to figure out what it does, and then go back and look at the else...
 
Chris Beckey
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java applications start with a main() method, java applets do not, nor do server components (EJBs, servlets, etc ...) or anything else that lives within a framework.
If you are trying to follow a Java application, then the main() method will be in the Main-class, which will be referenced in the manifest file if the application is packaged as an executable JAR file. If the application is just one class then finding the main() method is trivial.
If you are looking at the majority of modern java code though it will be written to live within a framework, which may be called from a native executable or a Java application or called through a native lib or some other of dozens of possibilities. To understand the startup of everything but applications you need to understand the lifecycle of components as defined by the framework they live within (EJB, servlet, portlet, etc ...).

 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

The answer to your question is the same as that to the old question about "How do you get to Carnegie Hall?"

I don't know of such an article myself, sorry. We call them methods, not functions and it's main not Main. Remember Java™ is case-sensitive.

You cannot tell the flow through a class; it is very unusual for a class to contain all the flow inside itself. What you can do is go through the individual methods and see what they do. That should be explained in the documentation comments already, so you can understand what a class does without knowing how it does it. you should be told which class to invoke to start the app. That class will have the main method in, and that method should ideally have a single statement in. It will have a new keyword, followed by a constructor invocation. The first part of that is the class name, so you can look at that class and read its constructor. Then there will be a method call. You have a look at that method, and see which other methods it calls.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!