How to read a java program.

Posted on 2014-02-19
Last Modified: 2014-03-10

I have a java program let us say 100 lines of code with main method, bunch of methods, variables static, non static etc.

Where shalli start scanning the code to understnad better. I scan top to bottom and forget what is at top and top to bottom forget some then i read from main. I wonder what is effective way to scan(read), understand a java class.

please advise
Any links resources ideas highly appreciated. Thanks in advance
Question by:gudii9
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
  • +1
LVL 19

Expert Comment

by:Ken Butters
ID: 39871592
I don't know of a resource that speaks to this particular issue... but first I'd look at main to get an basic understanding of what the purpose of the program is.

Next you need to look at any other classes included in the code to understand them separately.
Each of the other classes represent objects that have their own members and methods... so you have to understand them on their own.

For example if you have a simple basic program that uses Integers and Strings... you first have to understand what an Integer is, and what a String is... or their use in main will not be understood.

Once you understand your other classes (or objects) (like Integer / String in my simplified example)... then you can go back to main with a better understanding of the objects it creates and the things it does with those objects (methods).

A part of understanding what a class does is being able to reference JavaDocs to understand the built in java classes.

Any classes that you use that have javadoc documentation also have to be understood.  Any part of the program that is unclear will only add to your overall confusion and frustration.

So... I guess what I'm trying to say... is

Step 1 : break it up into bite size pieces and fully understand how each piece works.
Step 2 : step back and take a larger look at the whole program and understand how the peices work together.

One other thing... sometimes what you are coding in has prerequisites.   For example in this question you marked it as a Java question and a JSP question.  JSP's basically write HTML pages... so in order to understand JSP's you not only have to understand Java, but you have to understand HTML.  

With JSP's you are sort of wrting code (java)... that writes it's own code (HTML).... so obviously the developer must have a solid grasp of both technologies... or none if it will make any sense.
LVL 27

Accepted Solution

dpearson earned 250 total points
ID: 39874174
You should try to read the methods in the order they get used.

E.g. With code like this:

public void B { }

public static void main() {
   A() ;
   B() ;

public void C() { }

public void A() { C() ; }

You'd start with "main", see that it calls "A" - read that next.  See that "A" calls C so read that next.  Now you're done with C, so back to A.  It doesn't call anything else, so back to "main".

Next method in "main" is B, so read that next and so on.

In this example you'd read the code in the order:
(note this is very different from top to bottom or bottom to top).

That way you are piecing together what the program does in the order it will do it.  In this way you are reading the "story" of the code in the order it will run.  Which is an order that should make sense.

If you read top to bottom or anything else, it will be harder to understand as the code won't run in that order and may not make sense.

Hope that helps,


Author Comment

ID: 39878572
Adding to complexity some methods come from classes of jar files which do not open in eclipse. How to take them also into consideration?
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

LVL 35

Expert Comment

ID: 39878688
I am guessing that you mean that these are 3rd party jars and that you don't have access to the source code. At this point I would say that you shouldn't need the source code,  only for quite advanced stuff should you really need to look that deeply.

If your code is calling 3rd party jar classes/methods then youshould be able to find Javadoc documentation for these that should explain in enough detail what the method is doing.
LVL 27

Expert Comment

ID: 39878759
Yeah, like mccarl says at some point you need to just understand what a method does through its documentation, not by following the source code to the very bottom of every method.

If you encounter a method where there is no source code then you will just need to do your best to understand it from the docs (and what parameters it takes and returns, and it's name) and then proceed with the rest of the program.


Author Comment

ID: 39881495
Is there a built in decompiler to see the jar class file in proper way in eclipse. Most of the time that jar code class file code in eclipse is gibberish. Please advise
LVL 19

Expert Comment

by:Ken Butters
ID: 39881585
I agree with previous Experts... and no, there is not a built in decompiler in eclipse.

If you cannot find the documentation for a jar file, and you have no idea what it's purpose is, then you probably shouldn't be using it anyway.

Some jar files are compiled to protect the author's source code.  If you have legitimate access to the jar file, then you will be given or have available to you... the necessary documentation to use it correctly.
LVL 27

Expert Comment

ID: 39881641
Many times Jar files are distributed with a companion source jar file (at least for open source projects).  If you have the source jar file as well as the compiled code jar file then you can point Eclipse (or other IDEs) at the source jar file and it will show you the underlying source code.

There's more on how to do that here:

But this should only be used when you really need to delve into the details of how a library works.  For most libraries, you should be fine to just read the documentation and work with that alone (and ignore the source).

Author Comment

ID: 39886669
If there are is no main class in the java application then how should i proceed. Please advise
LVL 27

Expert Comment

ID: 39886724
Most large applications you won't start from the main method.  You should start instead from some point where a single operation would begin.  For a web app this would tend to be a web request (e.g. the code to implement a servlet).  For the database layer you could start with any point where a request comes in to insert or read a value from the database and trace down into where the request terminates at a call to the actual database server.

If you aren't sure where these major entry points are, you should ask somebody else who is already familiar with the code to help you find them.


Author Comment

ID: 39890164
Does making any drawing or mind map or picture helps in understanding and referrign myself later point quickly.

Since object oriented programming all about creating objects and invoking.

For example

package objets;
public class Scanner {
//class level variable
static int paperCount = 1;
//class level method
static void Scan() {
if (paperCount > 0) {
paperCount = paperCount - 1;
System.out.println("Document printed");
} else {
System.out.println("catridge is out");
//instance level method
void scanDoc() {

package objects;
public class ScannerTest {
public static void main(String args[]) {

Scanner p1 = new Scanner();
Scanner p2 = new Scanner();
// object level method

Open in new window

Draw picture as attached Rectangle Class with class level variables, methods.(something like objects interaction diagram how they objects communicates with each other let us say 10 of the objects in the application?)
Draw two eclipses for scanner instances p1, p2 with instance level methods may be from then join to other class method wherever call leads to?

Please advise
LVL 19

Expert Comment

by:Ken Butters
ID: 39890276
I think anything that helps you remember what the class members are, and what the class functions are, and how they work together is a fine idea.

Sometimes drawing it out can help you get your head around it.

Author Comment

ID: 39893887
any effective way or best practice of representing the object navigation for future reference. please advise
LVL 19

Assisted Solution

by:Ken Butters
Ken Butters earned 250 total points
ID: 39893900
You can draw it out however you like, but I think in the long run, becoming familiar with javadocs is the best way to go, because that is a documentation style that is a standard for quickly coming up to speed on a new class in java that you are not familiar with.

I think as you gain experience in Java... that reviewing javadocs will provide enough information and confidence to use new java classes.

The more complex classes usually include example usage... so that helps too.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
spring jars download 1 67
Facing this issue for maven proxy setting 2 43
glassfish admin console 1 47
Java Inheritance super keyword use 8 69
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question