Solved

How to read a java program.

Posted on 2014-02-19
14
769 Views
Last Modified: 2014-03-10
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
0
Comment
Question by:gudii9
  • 5
  • 4
  • 4
  • +1
14 Comments
 
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.
0
 
LVL 26

Accepted Solution

by:
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:
"main"
"A"
"C"
"B"
(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,

Doug
0
 
LVL 7

Author Comment

by:gudii9
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?
0
 
LVL 35

Expert Comment

by:mccarl
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.
0
 
LVL 26

Expert Comment

by:dpearson
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.

Doug
0
 
LVL 7

Author Comment

by:gudii9
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
0
 
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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 26

Expert Comment

by:dpearson
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:
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).
0
 
LVL 7

Author Comment

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

Expert Comment

by:dpearson
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.

Doug
0
 
LVL 7

Author Comment

by:gudii9
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() {
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
0
 
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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39893887
any effective way or best practice of representing the object navigation for future reference. please advise
0
 
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.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now