Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

How to read a java program.

Hi,

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
Avatar of Ken Butters
Ken Butters
Flag of United States of America image

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.
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

Adding to complexity some methods come from classes of jar files which do not open in eclipse. How to take them also into consideration?
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.
Avatar of dpearson
dpearson

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.

Doug
Avatar of gudii9

ASKER

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
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:
http://javarevisited.blogspot.com/2012/12/how-to-attach-source-in-eclipse-Jar-JDK-debugging.html

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).
Avatar of gudii9

ASKER

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.

Doug
Avatar of gudii9

ASKER

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() {
print();
}



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

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

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
ObjectInstancDiagram.jpg
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.
Avatar of gudii9

ASKER

any effective way or best practice of representing the object navigation for future reference. please advise
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial