How to read a java program.


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
Who is Participating?
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,

Ken ButtersCommented:
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.
gudii9Author Commented:
Adding to complexity some methods come from classes of jar files which do not open in eclipse. How to take them also into consideration?
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
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.
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.

gudii9Author Commented:
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
Ken ButtersCommented:
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.
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).
gudii9Author Commented:
If there are is no main class in the java application then how should i proceed. Please advise
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.

gudii9Author Commented:
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
Ken ButtersCommented:
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.
gudii9Author Commented:
any effective way or best practice of representing the object navigation for future reference. please advise
Ken ButtersCommented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.